远程服务器存储之JDK方式
生活随笔
收集整理的這篇文章主要介紹了
远程服务器存储之JDK方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.API
? ?1.new ?URL("http://網絡資源路徑")
? ?2.openConnection(),返回URLConnection對象
二.HttpURLConnection
? ?1.setRequestMethod(“GET”/“POST”):設置請求方式
? ?2.setConnectTimeout(毫秒數):設置連接超時時間
? ?3.setReadTimeout(毫秒數):設置讀超時時間
? ?4.connect( ):連接服務器,發送請求
? ?5.getOutputStream( ):得到連接輸出流,通過輸出流向服務器發送請求體
? ?6.getResponseCode( ):得到響應狀態碼,200代表正常響應
? ?7.getInputStream( ):得到連接輸入流,通過輸入流接收服務器發送的響應體
? ?8.disconnect( ):斷開連接
?
JDK方式的get和post代碼展示:
網絡訪問權限:
<uses-permission android:name="android.permission.INTERNET"/> 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp3.TestActivity3" 11 android:orientation="vertical"> 12 13 <LinearLayout 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content"> 16 17 <Button 18 android:layout_width="0dp" 19 android:layout_height="wrap_content" 20 android:layout_weight="1" 21 android:text="JDK-Get方式" 22 android:onClick="bt1_OnClick"/> 23 <Button 24 android:layout_width="0dp" 25 android:layout_height="wrap_content" 26 android:layout_weight="1" 27 android:text="JDK-Post方式" 28 android:onClick="bt2_OnClick"/> 29 </LinearLayout> 30 31 <!--<LinearLayout--> 32 <!--android:layout_width="match_parent"--> 33 <!--android:layout_height="wrap_content">--> 34 35 <!--<Button--> 36 <!--android:layout_width="0dp"--> 37 <!--android:layout_height="wrap_content"--> 38 <!--android:layout_weight="1"--> 39 <!--android:text="Android-Get方式"--> 40 <!--android:onClick="bt3_OnClick"/>--> 41 <!--<Button--> 42 <!--android:layout_width="0dp"--> 43 <!--android:layout_height="wrap_content"--> 44 <!--android:layout_weight="1"--> 45 <!--android:text="Android-Post方式"--> 46 <!--android:onClick="bt4_OnClick"/>--> 47 <!--</LinearLayout>--> 48 <EditText 49 android:layout_width="match_parent" 50 android:layout_height="200dp" 51 android:id="@+id/et_2"/> 52 53 </LinearLayout> .xml 1 package com.hanqi.testapp3; 2 3 import android.app.ProgressDialog; 4 import android.net.Uri; 5 import android.support.annotation.RequiresPermission; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.util.Log; 9 import android.view.View; 10 import android.widget.EditText; 11 import android.widget.Toast; 12 13 import org.apache.http.params.HttpConnectionParams; 14 import org.apache.http.params.HttpParams; 15 16 import java.io.InputStream; 17 import java.io.OutputStream; 18 import java.net.HttpURLConnection; 19 import java.net.URL; 20 21 public class TestActivity3 extends AppCompatActivity { 22 23 EditText et_2; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_test3); 29 30 et_2 = (EditText) findViewById(R.id.et_2); 31 32 } 33 34 // //JDK的Get方式 35 // public void bt1_OnClick(View v) 36 // { 37 // //1.進度對話框 38 // final ProgressDialog progressDialog=ProgressDialog.show(this,null,"正在加載,請稍候..."); 39 // 40 // //2.開啟子線程,訪問網絡 41 // new Thread(){ 42 // @Override 43 // public void run() { 44 // 45 // try { 46 // 47 // //1-URL 48 // URL url = new URL(et_2.getText().toString() + "?name=tom"); 49 // //URL url = new URL("http://www.sina.com.cn/" + "?name=tom"); 50 // 51 // //2-URL獲取連接 52 // HttpURLConnection huc=(HttpURLConnection)url.openConnection(); 53 // 54 // //請求方式 55 // huc.setRequestMethod("GET"); 56 // 57 // //設置超時 58 // huc.setConnectTimeout(3000); 59 // huc.setReadTimeout(3000); 60 // 61 // //連接并發送請求 62 // huc.connect(); 63 // 64 // //接收: 65 // //判斷返回狀態碼 200 66 // int code=huc.getResponseCode(); 67 // 68 // if (code==200) 69 // { 70 // //接收數據 71 // 72 // //輸入流: 73 // InputStream is=huc.getInputStream(); 74 // 75 // //讀取流 76 // 77 // //1-byte數組 78 // byte[] b=new byte[1024]; 79 // 80 // //2-讀到數組的長度 81 // int i=0; 82 // 83 // //3-數據 84 // final StringBuilder sbl=new StringBuilder(); 85 // 86 // while ((i=is.read(b))>0) 87 // { 88 // sbl.append(new String(b,0,i)); 89 // } 90 // 91 // //釋放資源 92 // is.close(); 93 // 94 // huc.disconnect(); 95 // 96 // //通過主線程顯示信息和關閉對話框 97 // runOnUiThread(new Runnable() { 98 // @Override 99 // public void run() { 100 // 101 // et_2.setText(sbl); 102 // 103 // progressDialog.dismiss(); 104 // } 105 // }); 106 // } 107 // else 108 // { 109 // Toast.makeText(TestActivity3.this, "連接錯誤,返回的狀態碼="+code, Toast.LENGTH_SHORT).show(); 110 // } 111 // 112 // 113 // }catch (Exception e){ 114 // 115 // e.printStackTrace(); 116 // 117 // progressDialog.dismiss(); 118 // 119 // } 120 // } 121 // }.start(); 122 // 123 // 124 // } 125 126 127 //顯示結果 128 String str = ""; 129 130 //JDK的Get方式 131 public void bt1_OnClick(View v) { 132 133 //1.啟動進度對話框 134 final ProgressDialog pd = ProgressDialog.show(this, null, "請稍候..."); 135 136 //2.啟動子線程,訪問網絡 137 new Thread() { 138 @Override 139 public void run() { 140 141 //訪問遠程服務器 142 //JDK Get 143 144 HttpURLConnection huc = null; 145 146 try { 147 //1.構造URL對象 148 URL url = new URL("Http://192.168.0.107:81/index.asp?name=mike&password=456"); 149 150 //2.得到HttpURLConnection 151 huc = (HttpURLConnection) url.openConnection(); 152 153 //3.設置HttpURLConnection 154 huc.setRequestMethod("GET"); 155 huc.setConnectTimeout(3000); 156 huc.setReadTimeout(3000); 157 158 //4.連接遠程服務器 159 huc.connect(); 160 161 //5.接收響應報文 162 int code = huc.getResponseCode(); 163 164 str = ""; 165 166 //6.判斷響應狀態碼是否=200 167 if (code == 200) { 168 //7.處理 169 //1 接收數據 170 171 //2 得到數據流 172 InputStream is = huc.getInputStream(); 173 174 byte[] b = new byte[1024]; 175 176 //讀到的數據長度 177 int i = 0; 178 179 while ((i = is.read(b)) > 0) { 180 //接收字符串 181 str += new String(b, 0, i); 182 183 } 184 185 is.close(); 186 187 } else { 188 189 str = "響應錯誤,錯誤碼=" + code; 190 } 191 192 193 //顯示結果,不能直接跨線程訪問主線程的視圖 194 runOnUiThread(new Runnable() { 195 @Override 196 public void run() { 197 198 et_2.setText(str); 199 200 201 } 202 }); 203 204 //支持跨線程訪問 205 pd.dismiss(); 206 207 208 } catch (Exception e) { 209 e.printStackTrace(); 210 211 //支持跨線程訪問 212 pd.dismiss(); 213 } finally { 214 //8.關閉連接和進度對話框 215 //釋放資源 216 if (huc != null) { 217 huc.disconnect(); 218 } 219 220 221 //支持跨線程訪問 222 pd.dismiss(); 223 } 224 } 225 }.start(); 226 } 227 228 //JDK的Post方式 229 public void bt2_OnClick(View v) { 230 231 //1.啟動進度對話框 232 final ProgressDialog pd = ProgressDialog.show(this, null, "請稍候..."); 233 234 //2.啟動子線程,訪問網絡 235 new Thread() { 236 @Override 237 public void run() { 238 239 //訪問遠程服務器 240 //JDK Post 241 242 HttpURLConnection huc = null; 243 244 try { 245 //1.構造URL對象 246 URL url = new URL("Http://192.168.0.107:81/index.asp"); 247 248 //2.得到HttpURLConnection 249 huc = (HttpURLConnection) url.openConnection(); 250 251 //3.設置HttpURLConnection 252 huc.setRequestMethod("POST"); 253 huc.setConnectTimeout(3000); 254 huc.setReadTimeout(3000); 255 256 //4.連接遠程服務器,輸出流 257 huc.connect(); 258 259 //數據放到請求體里 260 //1)得到輸出流 261 OutputStream os = huc.getOutputStream(); 262 263 String outstr = "name=tom&password=123"; 264 265 os.write(outstr.getBytes("UTF-8")); 266 267 os.close(); 268 269 Log.e("Tag", "發送..."); 270 271 //5.接收響應報文 272 int code = huc.getResponseCode(); 273 274 str = ""; 275 276 //6.判斷響應狀態碼是否=200 277 if (code == 200) { 278 Log.e("Tag", "接收..."); 279 //7.處理 280 //1 接收數據 281 282 //2 得到數據流,輸入流 283 InputStream is = huc.getInputStream(); 284 285 byte[] b = new byte[1024]; 286 287 //讀到的數據長度 288 int i = 0; 289 290 while ((i = is.read(b)) > 0) { 291 //接收字符串 292 str += new String(b, 0, i); 293 294 } 295 296 is.close(); 297 298 299 } else { 300 301 str = "響應錯誤,錯誤碼=" + code; 302 } 303 304 305 //顯示結果,不能直接跨線程訪問主線程的視圖 306 runOnUiThread(new Runnable() { 307 @Override 308 public void run() { 309 310 et_2.setText(str); 311 312 313 } 314 }); 315 316 //支持跨線程訪問 317 pd.dismiss(); 318 319 320 } catch (Exception e) { 321 e.printStackTrace(); 322 323 //支持跨線程訪問 324 pd.dismiss(); 325 } finally { 326 //8.關閉連接和進度對話框 327 //釋放資源 328 if (huc != null) { 329 huc.disconnect(); 330 } 331 332 333 //支持跨線程訪問 334 pd.dismiss(); 335 } 336 } 337 }.start(); 338 } 339 340 341 342 343 344 } .java?
?
轉載于:https://www.cnblogs.com/arxk/p/5582154.html
總結
以上是生活随笔為你收集整理的远程服务器存储之JDK方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第14周个人进度条
- 下一篇: ruby学习笔记(2)-chomp,ch