res/layout/main.xml的内容如下:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LINEARLAYOUT android:layout_height = "fill_parent" android:layout_width = "fill_parent" android:orientation = "vertical" xmlns:android = "http://schemas.android.com/apk/res/android" > |
03 | |
04 | < TEXTVIEW android:layout_height = "wrap_content" android:layout_width = "fill_parent" android:text = "网络连接测试" android:id = "@+id/TextView01" /> |
05 | |
06 | < BUTTON type = submit android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:text = "使用URLConnection访问GoogleWeatherAPI" android:id = "@+id/Button01" > |
07 | </ BUTTON > |
08 | |
09 | < BUTTON type = submit android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:text = "使用HttpClient访问GoogleWeatherAPI" android:id = "@+id/Button02" > |
10 | </ BUTTON > |
11 | |
12 | < SCROLLVIEW android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/ScrollView01" > |
13 | < TEXTVIEW android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/TextView02" > |
14 | </ TEXTVIEW > |
15 | </ SCROLLVIEW > |
16 | </ LINEARLAYOUT > |
3、MainActivity.java的内容如下:
001 | package android.basic.lesson30; |
002 | |
003 | import java.io.InputStreamReader; |
004 | import java.net.HttpURLConnection; |
005 | import java.net.URL; |
006 | |
007 | import org.apache.http.client.ResponseHandler; |
008 | import org.apache.http.client.methods.HttpGet; |
009 | import org.apache.http.impl.client.BasicResponseHandler; |
010 | import org.apache.http.impl.client.DefaultHttpClient; |
011 | |
012 | import android.app.Activity; |
013 | import android.os.Bundle; |
014 | import android.view.View; |
015 | import android.widget.Button; |
016 | import android.widget.TextView; |
017 | import android.widget.Toast; |
018 | |
019 | public class MainActivity extends Activity { |
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 | public void onCreate(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( new View.OnClickListener() { |
039 | @Override |
040 | public void onClick(View v) { |
041 | // 使用URLConnection连接GoogleWeatherAPI |
042 | urlConn(); |
043 | } |
044 | }); |
045 | |
046 | // 设置按钮单击监听器 |
047 | b2.setOnClickListener( new View.OnClickListener() { |
048 | @Override |
049 | public void onClick(View v) { |
050 | // 使用HttpCient连接GoogleWeatherAPI |
051 | httpClientConn(); |
052 | |
053 | } |
054 | }); |
055 | |
056 | } |
057 | |
058 | // 使用URLConnection连接GoogleWeatherAPI |
059 | protected void urlConn() { |
060 | |
061 | try { |
062 | // URL |
063 | URL url = new URL(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 = new InputStreamReader(httpconn.getInputStream(), "utf-8" ); |
072 | int i; |
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 | protected void httpClientConn() { |
094 | //DefaultHttpClient |
095 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
096 | //HttpGet |
097 | HttpGet httpget = new HttpGet(googleWeatherUrl2); |
098 | //ResponseHandler |
099 | ResponseHandler<STRING> responseHandler = new BasicResponseHandler(); |
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>
深入了解