android httpclient

来源:互联网 发布:数据有效性的使用方法 编辑:程序博客网 时间:2024/06/07 06:21
 res/layout/main.xml的内容如下:
 
01<?xmlversion="1.0"encoding="utf-8"?>
02<LINEARLAYOUTandroid:layout_height="fill_parent"android:layout_width="fill_parent"android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android">
03  
04<TEXTVIEWandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:text="网络连接测试"android:id="@+id/TextView01"/>
05  
06<BUTTONtype=submitandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:text="使用URLConnection访问GoogleWeatherAPI"android:id="@+id/Button01">
07</BUTTON>
08  
09<BUTTONtype=submitandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:text="使用HttpClient访问GoogleWeatherAPI"android:id="@+id/Button02">
10</BUTTON>
11  
12<SCROLLVIEWandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/ScrollView01">
13    <TEXTVIEWandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:id="@+id/TextView02">
14    </TEXTVIEW>
15</SCROLLVIEW>
16</LINEARLAYOUT>

3、MainActivity.java的内容如下:

 
001package android.basic.lesson30;
002  
003import java.io.InputStreamReader;
004import java.net.HttpURLConnection;
005import java.net.URL;
006  
007import org.apache.http.client.ResponseHandler;
008import org.apache.http.client.methods.HttpGet;
009import org.apache.http.impl.client.BasicResponseHandler;
010import org.apache.http.impl.client.DefaultHttpClient;
011  
012import android.app.Activity;
013import android.os.Bundle;
014import android.view.View;
015import android.widget.Button;
016import android.widget.TextView;
017import android.widget.Toast;
018  
019public class MainActivity extendsActivity {
020  
021    TextView tv;
022  
023    String googleWeatherUrl1 ="http://www.google.com/ig/api?weather=zhengzhou";
024    String googleWeatherUrl2 ="http://www.google.com/ig/api?hl=zh-cn&weather=zhengzhou";
025  
026    /** Called when the activity is first created. */
027    @Override
028    publicvoidonCreate(Bundle savedInstanceState) {
029        super.onCreate(savedInstanceState);
030        setContentView(R.layout.main);
031  
032        // 定义UI组件
033        Button b1 = (Button) findViewById(R.id.Button01);
034        Button b2 = (Button) findViewById(R.id.Button02);
035        tv = (TextView) findViewById(R.id.TextView02);
036  
037        // 设置按钮单击监听器
038        b1.setOnClickListener(newView.OnClickListener() {
039            @Override
040            publicvoidonClick(View v) {
041                // 使用URLConnection连接GoogleWeatherAPI
042                urlConn();
043            }
044        });
045  
046        // 设置按钮单击监听器
047        b2.setOnClickListener(newView.OnClickListener() {
048            @Override
049            publicvoidonClick(View v) {
050                // 使用HttpCient连接GoogleWeatherAPI
051                httpClientConn();
052  
053            }
054        });
055  
056    }
057  
058    // 使用URLConnection连接GoogleWeatherAPI
059    protectedvoidurlConn() {
060  
061        try{
062            // URL
063            URL url =newURL(googleWeatherUrl1);
064            // HttpURLConnection
065            HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
066  
067            if(httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
068                Toast.makeText(getApplicationContext(),"连接Google Weather API成功!",
069                        Toast.LENGTH_SHORT).show();
070                // InputStreamReader
071                InputStreamReader isr =newInputStreamReader(httpconn.getInputStream(),"utf-8");
072                inti;
073                String content ="";
074                // read
075                while((i = isr.read()) != -1) {
076                    content = content + (char) i;
077                }
078                isr.close();
079                //设置TextView
080                tv.setText(content);
081            }
082            //disconnect
083            httpconn.disconnect();
084  
085        }catch(Exception e) {
086            Toast.makeText(getApplicationContext(),"连接Google Weather API失败", Toast.LENGTH_SHORT)
087                    .show();
088            e.printStackTrace();
089        }
090    }
091  
092    // 使用HttpCient连接GoogleWeatherAPI
093    protectedvoidhttpClientConn() {
094        //DefaultHttpClient
095        DefaultHttpClient httpclient =newDefaultHttpClient();
096        //HttpGet
097        HttpGet httpget =newHttpGet(googleWeatherUrl2);
098        //ResponseHandler
099        ResponseHandler<STRING> responseHandler =newBasicResponseHandler();
100  
101        try{
102            String content = httpclient.execute(httpget, responseHandler);
103            Toast.makeText(getApplicationContext(),"连接Google Weather API成功!",
104                    Toast.LENGTH_SHORT).show();
105            //设置TextView
106            tv.setText(content);
107        }catch(Exception e) {
108            Toast.makeText(getApplicationContext(),"连接Google Weather API失败", Toast.LENGTH_SHORT)
109            .show();
110            e.printStackTrace();
111        }
112        httpclient.getConnectionManager().shutdown();
113    }
114}</STRING>

4、最后别忘了在AndroidManifest.xml中加入访问网络的权限,<uses-permission android:name="android.permission.INTERNET"></uses-permission>

 

 

深入了解

http://topmanopensource.iteye.com/blog/821964