一.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>
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 }
原文:http://www.cnblogs.com/arxk/p/5582154.html