Android HttpClient网络通信

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <Button android:text="GET" android:id="@+id/Button01"  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content">  
  8.     </Button>  
  9.     <Button android:text="POST" android:id="@+id/Button02"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content">  
  12.     </Button>  
  13.     <TextView android:id="@+id/TextView" android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"/>  
  15. </LinearLayout>  


  1. package com.Aina.Android;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.io.UnsupportedEncodingException;  
  8. import java.net.HttpURLConnection;  
  9. import java.net.MalformedURLException;  
  10. import java.net.URL;  
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13.   
  14. import org.apache.http.HttpEntity;  
  15. import org.apache.http.HttpResponse;  
  16. import org.apache.http.HttpStatus;  
  17. import org.apache.http.NameValuePair;  
  18. import org.apache.http.client.ClientProtocolException;  
  19. import org.apache.http.client.HttpClient;  
  20. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  21. import org.apache.http.client.methods.HttpGet;  
  22. import org.apache.http.client.methods.HttpPost;  
  23. import org.apache.http.impl.client.DefaultHttpClient;  
  24. import org.apache.http.message.BasicNameValuePair;  
  25. import org.apache.http.util.EntityUtils;  
  26.   
  27. import android.app.Activity;  
  28. import android.os.Bundle;  
  29. import android.os.Handler;  
  30. import android.os.Message;  
  31. import android.view.View;  
  32. import android.widget.Button;  
  33. import android.widget.TextView;  
  34.   
  35. public class Test extends Activity implements Runnable{  
  36.     /** Called when the activity is first created. */  
  37.     private Button btn_get = null;  
  38.     private Button btn_post = null;  
  39.     private TextView tv_rp = null;  
  40.     @Override  
  41.     public void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.main);  
  44.         btn_get = (Button) this.findViewById(R.id.Button01);  
  45.         btn_post = (Button) this.findViewById(R.id.Button02);  
  46.         tv_rp = (TextView) this.findViewById(R.id.TextView);  
  47.         btn_get.setOnClickListener(new Button.OnClickListener(){  
  48.   
  49.             public void onClick(View v) {  
  50.                 // TODO Auto-generated method stub  
  51.                 String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp?par=request-get";  
  52.                 HttpGet request = new HttpGet(httpUrl);  
  53.                 HttpClient httpClient = new DefaultHttpClient();  
  54.                 try {  
  55.                     HttpResponse response = httpClient.execute(request);  
  56.                     if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){  
  57.                         String str = EntityUtils.toString(response.getEntity());  
  58.                         tv_rp.setText(str);  
  59.                     }else{  
  60.                         tv_rp.setText("请求错误");  
  61.                     }   
  62.                 } catch (ClientProtocolException e) {  
  63.                     // TODO Auto-generated catch block  
  64.                     e.printStackTrace();      
  65.                 } catch (IOException e) {  
  66.                     // TODO Auto-generated catch block  
  67.                     e.printStackTrace();  
  68.                 }  
  69.             }  
  70.               
  71.         });  
  72.         btn_post.setOnClickListener(new Button.OnClickListener(){  
  73.   
  74.             public void onClick(View v) {  
  75.                 // TODO Auto-generated method stub  
  76.                 String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp";  
  77.                 HttpPost request = new HttpPost(httpUrl);  
  78.                 List<NameValuePair> params = new ArrayList<NameValuePair>();  
  79.                 params.add(new BasicNameValuePair("par","request-post"));  
  80.                 try {  
  81.                     HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");  
  82.                     request.setEntity(entity);  
  83.                     HttpClient client = new DefaultHttpClient();  
  84.                     HttpResponse response = client.execute(request);  
  85.                     if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){  
  86.                         String str = EntityUtils.toString(response.getEntity());  
  87.                         tv_rp.setText(str);  
  88.                     }else{  
  89.                         tv_rp.setText("请求错误");  
  90.                     }  
  91.                 } catch (UnsupportedEncodingException e) {  
  92.                     // TODO Auto-generated catch block  
  93.                     e.printStackTrace();  
  94.                 } catch (ClientProtocolException e) {  
  95.                     // TODO Auto-generated catch block  
  96.                     e.printStackTrace();  
  97.                 } catch (IOException e) {  
  98.                     // TODO Auto-generated catch block  
  99.                     e.printStackTrace();  
  100.                 }  
  101.             }  
  102.               
  103.         });  
  104.         new Thread(this).start();  
  105.     }  
  106.     public void refresh(){  
  107.         String httpUrl = "http://192.168.0.132:8080/Android/httpreq.jsp";  
  108.         try {  
  109.             URL url = new URL(httpUrl);  
  110.             HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();  
  111.             urlConn.connect();  
  112.             InputStream input = urlConn.getInputStream();  
  113.             InputStreamReader inputreader = new InputStreamReader(input);  
  114.             BufferedReader reader = new BufferedReader(inputreader);  
  115.             String str = null;  
  116.             StringBuffer sb = new StringBuffer();  
  117.             while((str = reader.readLine())!= null){  
  118.                 sb.append(str).append("/n");  
  119.             }  
  120.             if(sb != null){  
  121.                 tv_rp.setText(sb.toString());  
  122.             }else{  
  123.                 tv_rp.setText("NULL");  
  124.             }  
  125.             reader.close();  
  126.             inputreader.close();  
  127.             input.close();  
  128.             reader = null;  
  129.             inputreader = null;  
  130.             input = null;  
  131.         } catch (MalformedURLException e) {  
  132.             e.printStackTrace();  
  133.         } catch (IOException e) {  
  134.             // TODO Auto-generated catch block  
  135.             e.printStackTrace();  
  136.         }  
  137.     }  
  138.     public Handler handler = new Handler(){  
  139.         public void handleMessage(Message msg){  
  140.             super.handleMessage(msg);  
  141.             refresh();  
  142.         }  
  143.     };  
  144.     public void run() {  
  145.         // TODO Auto-generated method stub  
  146.         while(true){  
  147.             try {  
  148.                 Thread.sleep(1000);  
  149.                 handler.sendMessage(handler.obtainMessage());  
  150.             } catch (InterruptedException e) {  
  151.                 // TODO Auto-generated catch block  
  152.                 e.printStackTrace();  
  153.             }  
  154.         }  
  155.     }  
  156. }  


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.Aina.Android"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".Test"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   
  15.     </application>  
  16. <uses-permission android:name="android.permission.INTERNET" />  
  17.   
  18. </manifest>   

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值