Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209725
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-08 16:50:49

04_08_HTTP操作(二)







点击(此处)折叠或打开

  1. package org.marsdroid.http01;

  2. import java.io.BufferedReader;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;

  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.impl.client.DefaultHttpClient;

  10. import android.app.Activity;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;

  15. public class MainActivity extends Activity {
  16.     private Button requestButton = null;
  17.     private HttpResponse httpResponse = null;
  18.     private HttpEntity httpEntity = null;
  19.     /** Called when the activity is first created. */
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.main);
  24.         
  25.         requestButton = (Button)findViewById(R.id.requestButton);
  26.         requestButton.setOnClickListener(new OnClickListener() {
  27.             
  28.             /*
  29.              * 1、生成一个请求对象HttpGet: httpGet = new HttpGet("")
  30.              * 2、生成一个Http客户端对象: httpClient = new DefaultHttpClient();
  31.              * 3、使用Http客户端发送请求对象,得到服务器响应: httpResponse = httpClient.execute(httpGet);
  32.              * 4、从服务器响应中得到内容实体:httpEntity = httpResponse.getEntity();
  33.              * 5、从内容实体中得到输入流:inputSream = httpEntity.getContent();
  34.              * 6、把输入流变为读入流: new InputStreamReader(inputStram)
  35.              * 7、把读入流变为读入缓冲 reader=new BufferedReader(new InputStreamReader(inputSteam))
  36.              * 一般是: 输入流InputSteam->读入流InputStreamReader->读缓冲BufferedReader
  37.              * 8、从读缓冲中读字符串数据,每次读一行:line = reader.readLine()*/
  38.             @Override
  39.             public void onClick(View v) {
  40.                 //生成一个请求对象
  41.                 HttpGet httpGet = new HttpGet("");
  42.                 //生成一个Http客户端对象
  43.                 HttpClient httpClient = new DefaultHttpClient();
  44.                 //使用Http客户端发送请求对象
  45.                 InputStream inputStream = null;
  46.                 try {
  47.                     httpResponse = httpClient.execute(httpGet);
  48.                     httpEntity = httpResponse.getEntity();
  49.                     inputStream = httpEntity.getContent();
  50.                     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  51.                     String result = "";
  52.                     String line = "";
  53.                     while((line = reader.readLine()) != null){
  54.                         result = result + line;
  55.                     }
  56.                     System.out.println(result);
  57.                 } catch (Exception e) {
  58.                     // TODO Auto-generated catch block
  59.                     e.printStackTrace();
  60.                 }
  61.                 finally{
  62.                     try{
  63.                         inputStream.close();
  64.                     }
  65.                     catch(Exception e){
  66.                         e.printStackTrace();
  67.                     }
  68.                 }
  69.             }
  70.         });
  71.     }
  72. }

别忘了再AndroidMainfest.xml中添加 访问互联网的权限:



点击(此处)折叠或打开

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




阅读(640) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~