Android网络请求通经常使用的是HttpURLConnection类和HttpClient类。HttpURLConnection类是由java提供的网络訪问请求类。而HttpClient类是由apache提供的。它相当于是一个增强版本号的HttpURLConnection类,换句话来说,HttpURLConnection能够做的事情HttpClient所有能够做。
因为网络请求在Android中是比較耗时的,所以不能放在主线程中,要不然会出现ANR的问题,因此网络请求通常放在线程中进行,这样就不会堵塞UI线程。能够更有利于用户的交互。下面实例涉及到对JSON数据的解析,假设不清楚的能够到点击打开链接
以下是HttpClient get请求的方式
获取网络数据的线程类
public class GetData extends Thread { Handler handler; String url; public GetData(Handler handler, String url) { this.handler = handler; this.url = url; } @Override public void run() { // TODO Auto-generated method stub super.run(); HttpGet httpRequest = new HttpGet(url);// 建立http get联机 HttpResponse httpResponse; String result = null; try { httpResponse = new DefaultHttpClient().execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == 200) result = EntityUtils.toString(httpResponse.getEntity(), "GBK"); //获取对应的字符串 try { JSONTokener jsonParser = new JSONTokener(result); JSONObject data = (JSONObject) jsonParser.nextValue(); JSONArray dataArray = data.getJSONArray("data"); /*for(int t=0; t<dataArray.length(); ++t){ JSONObject obj = (JSONObject) dataArray.get(t); String s1 = (String) obj.get("data"); String s2 = (String) obj.get("content"); String s3 = obj.getString("img"); Log.e("huang", s1+" "+s2+" "+s3); }*/ JSONObject obj = (JSONObject) dataArray.get(0); String s1 = (String) obj.get("data"); String s2 = (String) obj.get("content"); String s3 = obj.getString("img"); //获取图片 HttpGet get = new HttpGet(s3); HttpResponse res = new DefaultHttpClient().execute(get); InputStream in = res.getEntity().getContent(); Bitmap img = BitmapFactory.decodeStream(in); Message msg = new Message(); Bundle bundle = new Bundle(); bundle.putString("s1", s1); bundle.putString("s2", s2); msg.obj = img; msg.setData(bundle); handler.sendMessage(msg); } catch (JSONException ex) { // 异常处理代码 } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }// 发出http请求 } }
UI线程
public class Main extends Activity { Handler handler; TextView text, text1; ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.text); text1 = (TextView) findViewById(R.id.text1); img = (ImageView) findViewById(R.id.img); handler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Bundle b = msg.getData(); Log.e("huang", b.getString("s1")+" "+b.getString("s2")); text.setText(b.getString("s1")); text1.setText(b.getString("s2")); img.setImageBitmap((Bitmap) msg.obj); } }; new GetData(handler, "http://7xiwtn.com1.z0.glb.clouddn.com/data.jason.txt").start(); } }
执行结果:
以下是HttpClient post请求的方式
事实上post请求跟get请求几乎相同,仅仅只是在一些地方不同,一般像上传数据、提交数据一般都是用post请求,由于get请求是将參数明文加入在訪问的url上,发送的数据大小有限。不安全,而post请求发送的数据量一般能够非常大,安全。
public class PostData extends Thread { String url; public PostData(String url) { this.url = url; } @Override public void run() { // TODO Auto-generated method stub super.run(); HttpPost post = new HttpPost(url); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("mobileNo", "13826040804")); //post.addHeader("platform", "android"); try { post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpResponse response = new DefaultHttpClient().execute(post); if(response.getStatusLine().getStatusCode() == 200){ String result = EntityUtils.toString(response.getEntity()); Log.e("huang", result); /*JSONTokener token = new JSONTokener(result); JSONObject obj = (JSONObject) token.nextValue(); JSONObject obj1 = obj.getJSONObject("result"); boolean check = obj1.getBoolean("IsSuccess"); Log.e("huang", "isSuccess:"+check);*/ } } catch (Exception e) { // TODO Auto-generated catch block Log.e("huang", "error"); e.printStackTrace(); } } }