package com.jiahui.net;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class HTTPDemoActivity extends Activity {
private Button getBtn, postBtn, imageBtn;
private EditText urlText, imageUrlText;
private TextView resutlView;
private ImageView imageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
urlText = (EditText) findViewById(R.id.urlText);
imageUrlText = (EditText) findViewById(R.id.imageurlText);
resutlView = (TextView) findViewById(R.id.resultView);
getBtn = (Button) findViewById(R.id.getBtn);
postBtn = (Button) findViewById(R.id.postBtn);
imageBtn = (Button) findViewById(R.id.imgBtn);
imageView = (ImageView) findViewById(R.id.imgeView01);
getBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(urlText.getText().toString());
resutlView
.setText(request("GET", urlText.getText().toString()));
}
});
postBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println(urlText.getText().toString());
resutlView
.setText(request("POST", urlText.getText().toString()));
}
});
imageBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getImage(imageUrlText.getText().toString());
}
});
}
private String request(String method, String url) {
HttpResponse httpResponse = null;
StringBuffer result = new StringBuffer();
try {
if (method.equals("GET")) {
// 1.通过url创建HttpGet对象
HttpGet httpGet = new HttpGet(url);
// 2.通过DefaultClient的excute方法执行返回一个HttpResponse对象
HttpClient httpClient = new DefaultHttpClient();
httpResponse = httpClient.execute(httpGet);
// 3.取得相关信息
// 取得HttpEntiy
HttpEntity httpEntity = httpResponse.getEntity();
// 得到一些数据
// 通过EntityUtils并指定编码方式取到返回的数据
result.append(EntityUtils.toString(httpEntity, "utf-8"));
//得到StatusLine接口对象
StatusLine statusLine = httpResponse.getStatusLine();
//得到协议
;
result.append("协议:" + statusLine.getProtocolVersion() + "\r\n");
int statusCode = statusLine.getStatusCode();
result.append("状态码:" + statusCode + "\r\n");
} else if (method.equals("POST")) {
// 1.通过url创建HttpGet对象
HttpPost httpPost = new HttpPost(url);
// 2.通过DefaultClient的excute方法执行返回一个HttpResponse对象
HttpClient httpClient = new DefaultHttpClient();
httpResponse = httpClient.execute(httpPost);
// 3.取得相关信息
// 取得HttpEntiy
HttpEntity httpEntity = httpResponse.getEntity();
// 得到一些数据
// 通过EntityUtils并指定编码方式取到返回的数据
result.append(EntityUtils.toString(httpEntity, "utf-8"));
StatusLine statusLine = httpResponse.getStatusLine();
statusLine.getProtocolVersion();
int statusCode = statusLine.getStatusCode();
result.append("状态码:" + statusCode + "\r\n");
}
} catch (Exception e) {
Toast.makeText(HTTPDemoActivity.this, "网络连接异常", Toast.LENGTH_LONG)
.show();
}
return result.toString();
}
public void getImage(String url) {
try {
// 1.通过url创建HttpGet对象
HttpGet httpGet = new HttpGet(url);
// 2.通过DefaultClient的excute方法执行返回一个HttpResponse对象
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
// 3.取得相关信息
// 取得HttpEntiy
HttpEntity httpEntity = httpResponse.getEntity();
// 4.通过HttpEntiy.getContent得到一个输入流
InputStream inputStream = httpEntity.getContent();
System.out.println(inputStream.available());
//通过传入的流再通过Bitmap工厂创建一个Bitmap
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//设置imageView
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
Toast.makeText(HTTPDemoActivity.this, "网络连接异常", Toast.LENGTH_LONG)
.show();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
深入Android通过Apache HTTP访问HTTP资源示例代码
共35个文件
class:11个
xml:7个
java:3个
4星 · 超过85%的资源 需积分: 11 306 下载量 54 浏览量
2011-10-17
13:39:23
上传
评论 1
收藏 76KB ZIP 举报
温馨提示
深入Android通过Apache HTTP访问HTTP资源示例代码 深入Android通过Apache HTTP访问HTTP资源示例代码
资源推荐
资源详情
资源评论
收起资源包目录
深入Android通过Apache HTTP访问HTTP资源.zip (35个子文件)
深入Android通过Apache HTTP访问HTTP资源
HTTPDemo
.project 844B
proguard.cfg 1KB
src
com
jiahui
net
HTTPDemoActivity.java 5KB
HttpUtil.java 63B
AndroidManifest.xml 755B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
main.xml 2KB
values
strings.xml 175B
assets
gen
com
jiahui
net
R.java 1KB
default.properties 362B
.classpath 280B
bin
resources.ap_ 11KB
com
jiahui
net
R$layout.class 382B
R$drawable.class 388B
R$attr.class 328B
HTTPDemoActivity$2.class 1KB
HTTPDemoActivity.class 5KB
HttpUtil.class 279B
R$id.class 568B
R.class 503B
R$string.class 415B
HTTPDemoActivity$3.class 1KB
HTTPDemoActivity$1.class 1KB
HTTPDemo.apk 16KB
classes.dex 7KB
Servers
.project 211B
.settings
org.eclipse.wst.server.core.prefs 112B
Tomcat v7.0 Server at localhost-config
tomcat-users.xml 1KB
catalina.policy 10KB
catalina.properties 5KB
context.xml 1KB
web.xml 51KB
server.xml 6KB
共 35 条
- 1
90worker
- 粉丝: 864
- 资源: 64
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页