URLConnection是个抽象类,它有两个直接子类分别是HttpURLConnection和JarURLConnection。另外一个重要的类是URL,通常URL可以通过传给构造器一个String类型的参数来生成一个指向特定地址的URL实例。
JDK自带的请求方式,包名: java.net.HttpURLConnection;
HttpURLConnection请求的类别:
分为二类,GET与POST请求。二者的区别在于: 1: get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet, 2: post与get的不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。效果图:
1 public class MainActivity extends Activity implements OnClickListener { 2 public static String urls = "http://bajie.zhangwoo.cn/app.php?platform=android&appkey=5a379b5eed8aaae531df5f60b12100cfb6dff2c1"; 3 4 private TextView mTextView; 5 private ImageView imagegvoew; 6 String resultStr = ""; 7 String resultStr1 = ""; 8 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 initView(); 14 } 15 16 private void initView() { 17 findViewById(R.id.btn1).setOnClickListener(this); 18 findViewById(R.id.btn2).setOnClickListener(this); 19 findViewById(R.id.btn3).setOnClickListener(this); 20 mTextView = (TextView) findViewById(R.id.Text); 21 imagegvoew = (ImageView) findViewById(R.id.imagegvoew); 22 23 } 24 25 @Override 26 public void onClick(View v) { 27 switch (v.getId()) { 28 case R.id.btn1: 29 Thread visitBaiduThread = new Thread(new VisitWebRunnable()); 30 visitBaiduThread.start(); 31 try { 32 visitBaiduThread.join(); 33 if (!resultStr.equals("")) { 34 mTextView.setText(resultStr); 35 } 36 } catch (InterruptedException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 break; 41 case R.id.btn2: 42 43 Thread visitBaiduThreads = new Thread(new VisitWebRunnables()); 44 visitBaiduThreads.start(); 45 try { 46 visitBaiduThreads.join(); 47 if (!resultStr1.equals("")) { 48 mTextView.setText(resultStr1); 49 } 50 } catch (InterruptedException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 break; 55 case R.id.btn3: 56 new Thread(new Runnable() { 57 58 @Override 59 public void run() { 60 // TODO Auto-generated method stub 61 new DownImgAsyncTask() 62 .execute("http://avatar.csdn.net/8/6/0/2_dickyqie.jpg"); 63 } 64 }).start(); 65 66 break; 67 default: 68 break; 69 } 70 71 } 72 73 class DownImgAsyncTask extends AsyncTask{ 74 75 @Override 76 protected void onPreExecute() { 77 // TODO Auto-generated method stub 78 super.onPreExecute(); 79 80 } 81 82 @Override 83 protected Bitmap doInBackground(String... params) { 84 // TODO Auto-generated method stub 85 Bitmap b = Util.getImageBitmap(params[0]); 86 return b; 87 } 88 89 @Override 90 protected void onPostExecute(Bitmap result) { 91 // TODO Auto-generated method stub 92 super.onPostExecute(result); 93 if (result != null) { 94 imagegvoew.setImageBitmap(result); 95 } 96 } 97 98 } 99 100 class VisitWebRunnable implements Runnable {101 102 @Override103 public void run() {104 // TODO Auto-generated method stub105 String data = Util.get(urls);106 resultStr = data;107 }108 109 }110 111 class VisitWebRunnables implements Runnable {112 113 @Override114 public void run() {115 // TODO Auto-generated method stub116 Map params = new HashMap ();117 params.put("q", "test");118 params.put("showapi_appid", "11548");119 params.put("showapi_timestamp", "20160511151954");120 params.put("showapi_sign", "bb1d15ab7ce646ec87cc89d684ca4bcb");121 String data = Util.post("https://route.showapi.com/32-9", params);122 resultStr1 = data;123 }124 125 }126 }
- 注意:使用时常出现出现NetworkOnMainThreadException错误,Android.os.NetworkOnMainThreadException错误提示的原因
- 原因:不允许在主线程中进行网络访问
- 解决:将网络访问的操作单独放到一个线程中
记得加网络权限
源码点击下载: