package mars.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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 HttpActivity extends Activity {
private Button requestButton = null;
HttpResponse httpResponse = null;
HttpEntity httpEntity = null;
InputStream inputStream = null;
BufferedReader bufferedReader = 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() {
@Override
public void onClick(View v) {
String url = "http://www.baidu.com";
HttpGet httpGet = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();
try {
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream));
StringBuffer result = new StringBuffer();
String temp = "";
while ((temp = bufferedReader.readLine()) != null) {
result.append(temp);
}
System.out.println(result.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}
如果ActivityManifest中的minSDK
<uses-sdk android:minSdkVersion="14" />
配置如果写成 14 就会报异常。改为14一下就正常。不知道是不是bug
分享到:
相关推荐
其中有4.1.1jar包、httpclient说明文档doc格式、简单使用例子post\get方式都有,导入类可用。项目中使用很方便!
httpclient-4.0.jar, httpclient-4.0.jar, httpclient-4.0.jar
httpclient4.0
httpclient4.0所需jar包
httpclient4.0源文件,httpclient4.0源文件,httpclient4.0源文件
java 使用httpclient访问servlet,方便两个不同服务器上的程序相互调用.
httpclient-4.2.5.jar --http请求 jettison.jar --组装解析Json ojdbc7.jar --数据库操作 reportng-1.1.4.jar -- testng测试报告美化插件 velocity-dep-1.4.jar -... guice-4.0.jar -- reportng-1.1.4.jar的依赖库
通过httpclient的方式获取返回的文本,也可以访问某些支持http方式访问的接口,返回XML并解析
HttpClient代理访问网络
Android使用HttpClient发送请求、接收响应很简单,只要如下几步即可: Step1:创建HttpClient对象; Step2:如果需要发送GET请求,则创建HttpGet对象; 如果需要发送POST请求,则创建HttpPost对象; Step3:如果...
httpclient-4.0-beta1.jar
本实例描述了Android平台下使用Apache开源项目的HttpClient访问网络的示例。分别使用HttpClient下的两个子实现类,DefaultHttpClient和AndroidHttpClient分别进行演示。
httpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jarhttpclient-4.0-beta2.jar
NULL 博文链接:https://yhz61010.iteye.com/blog/654678
使用HttpClient必须的jar包 使用HttpClient必须的jar包 使用HttpClient必须的jar包
Http或HttpClient访问网路数据
1.含有官方HttpClient4.0的jar包 2.有之前HttpClient3.0的jar包,就是import importorg.apache.commons.*; 3.包含jsoup,解析HTML的jar包 4.包含jcookie.jar及jtidy-r938.jar的jar包.
android HttpClient 获取网络图片 实例
使用httpclient和httpsurlconnection两种方法访问https网址,同时分为验证证书和不验证证书两种方法