04_08_HTTP操作(二)
-
package org.marsdroid.http01;
-
-
import java.io.BufferedReader;
-
import java.io.InputStream;
-
import java.io.InputStreamReader;
-
-
import org.apache.http.HttpEntity;
-
import org.apache.http.HttpResponse;
-
import org.apache.http.client.HttpClient;
-
import org.apache.http.client.methods.HttpGet;
-
import org.apache.http.impl.client.DefaultHttpClient;
-
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.View.OnClickListener;
-
import android.widget.Button;
-
-
public class MainActivity extends Activity {
-
private Button requestButton = null;
-
private HttpResponse httpResponse = null;
-
private HttpEntity httpEntity = null;
-
/** Called when the activity is first created. */
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
requestButton = (Button)findViewById(R.id.requestButton);
-
requestButton.setOnClickListener(new OnClickListener() {
-
-
/*
-
* 1、生成一个请求对象HttpGet: httpGet = new HttpGet("")
-
* 2、生成一个Http客户端对象: httpClient = new DefaultHttpClient();
-
* 3、使用Http客户端发送请求对象,得到服务器响应: httpResponse = httpClient.execute(httpGet);
-
* 4、从服务器响应中得到内容实体:httpEntity = httpResponse.getEntity();
-
* 5、从内容实体中得到输入流:inputSream = httpEntity.getContent();
-
* 6、把输入流变为读入流: new InputStreamReader(inputStram)
-
* 7、把读入流变为读入缓冲 reader=new BufferedReader(new InputStreamReader(inputSteam))
-
* 一般是: 输入流InputSteam->读入流InputStreamReader->读缓冲BufferedReader
-
* 8、从读缓冲中读字符串数据,每次读一行:line = reader.readLine()*/
-
@Override
-
public void onClick(View v) {
-
//生成一个请求对象
-
HttpGet httpGet = new HttpGet("");
-
//生成一个Http客户端对象
-
HttpClient httpClient = new DefaultHttpClient();
-
//使用Http客户端发送请求对象
-
InputStream inputStream = null;
-
try {
-
httpResponse = httpClient.execute(httpGet);
-
httpEntity = httpResponse.getEntity();
-
inputStream = httpEntity.getContent();
-
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
-
String result = "";
-
String line = "";
-
while((line = reader.readLine()) != null){
-
result = result + line;
-
}
-
System.out.println(result);
-
} catch (Exception e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
finally{
-
try{
-
inputStream.close();
-
}
-
catch(Exception e){
-
e.printStackTrace();
-
}
-
}
-
}
-
});
-
}
-
}
别忘了再AndroidMainfest.xml中添加 访问互联网的权限:
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android=""
-
package="org.marsdroid.http01"
-
android:versionCode="1"
-
android:versionName="1.0">
-
<uses-sdk android:minSdkVersion="10" />
-
<uses-permission android:name="android.permission.INTERNET"/>
-
<application android:icon="@drawable/icon" android:label="@string/app_name">
-
<activity android:name=".MainActivity"
-
android:label="@string/app_name">
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
-
</application>
-
</manifest>
阅读(640) | 评论(0) | 转发(0) |