本讲内容:URLConnection和HttpClient使用入门
在Android中除了使用WebView控件访问网络以外,还有用代码方式访问网络的方法,代码方式有时候会显得更加灵活。本讲会介绍使用URLConnection对象和HttpClient组件访问网络的方法。而这两种方法和Java Web开发中的使用方式几乎没有区别,而Web开发的相关资料比比皆是,因此有兴趣的同学学完本讲之后可以专门去研究一下HttpClient4.0的内容,以求更深入的学习。
一、分别使用URLConnection和HttpClient访问Google天气服务的例子
这个例子的的目的就是从Google哪里获取郑州的天气预报信息,并显示在TextView中,本讲只会把返回的XML数据显示出来,下一讲我们学XML解析的时候再把这个天气预报做成图文并茂的形式,所以大家先暂时忍耐一下丑陋的界面。
1、新建一个项目 Lesson30_HttpClient ,主Activity的文件名是 MainActivity.java
2、res/layout/main.xml的内容如下:
01 | <?xml version="1.0" encoding="utf-8"?> |
04 | <textview android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="网络连接测试"> |
06 | <button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用URLConnection访问GoogleWeatherAPI"> |
09 | <button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用HttpClient访问GoogleWeatherAPI"> |
12 | <scrollview android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> |
13 | <textview android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"> |
16 | </textview></linearlayout> |
3、MainActivity.java的内容如下:
001 | package android.basic.lesson30; |
003 | import java.io.InputStreamReader; |
004 | import java.net.HttpURLConnection; |
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; |
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; |
019 | public class MainActivity extends Activity { |
026 | /** Called when the activity is first created. */ |
028 | public void onCreate(Bundle savedInstanceState) { |
029 | super.onCreate(savedInstanceState); |
030 | setContentView(R.layout.main); |
033 | Button b1 = (Button) findViewById(R.id.Button01); |
034 | Button b2 = (Button) findViewById(R.id.Button02); |
035 | tv = (TextView) findViewById(R.id.TextView02); |
038 | b1.setOnClickListener(new View.OnClickListener() { |
040 | public void onClick(View v) { |
047 | b2.setOnClickListener(new View.OnClickListener() { |
049 | public void onClick(View v) { |
059 | protected void urlConn() { |
063 | URL url = new URL(googleWeatherUrl1); |
065 | HttpURLConnection httpconn = (HttpURLConnection) url.openConnection(); |
067 | if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) { |
068 | Toast.makeText(getApplicationContext(), "连接Google Weather API成功!", |
069 | Toast.LENGTH_SHORT).show(); |
071 | InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(), "utf-8"); |
075 | while ((i = isr.read()) != -1) { |
076 | content = content + (char) i; |
083 | httpconn.disconnect(); |
085 | } catch (Exception e) { |
086 | Toast.makeText(getApplicationContext(), "连接Google Weather API失败", Toast.LENGTH_SHORT) |
093 | protected void httpClientConn() { |
095 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
097 | HttpGet httpget = new HttpGet(googleWeatherUrl2); |
099 | ResponseHandler<string> responseHandler = new BasicResponseHandler(); |
102 | String content = httpclient.execute(httpget, responseHandler); |
103 | Toast.makeText(getApplicationContext(), "连接Google Weather API成功!", |
104 | Toast.LENGTH_SHORT).show(); |
107 | } catch (Exception e) { |
108 | Toast.makeText(getApplicationContext(), "连接Google Weather API失败", Toast.LENGTH_SHORT) |
112 | httpclient.getConnectionManager().shutdown(); |
4、最后别忘了在AndroidManifest.xml中加入访问网络的权限,<uses-permission android:name="android.permission.INTERNET"></uses-permission>
5、运行程序查看结果:
按第一个按钮的效果,返回的数据结果显示在了TextView里。
按第二个按钮的效果,返回的数据结果显示在了TextView里, 所不同的是显示的是中文。
好了,本讲先到这里。
转自 http://android.yaohuiji.com/archives/932