package com.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.HttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class HttpActivity extends Activity {
private Button mButton1, mButton2;
private TextView mTextView1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mButton2 = (Button) findViewById(R.id.myButton2);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mButton1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String uriAPI = "http://www.sina.com";
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str", "post string"));
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params,
HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String strResult = EntityUtils.toString(httpResponse
.getEntity());
mTextView1.setText(strResult);
} else {
mTextView1.setText("Error Response: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
mButton2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String uriAPI = "http://www.sina.com";
HttpGet httpRequest = new HttpGet(uriAPI);
try {
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String strResult = EntityUtils.toString(httpResponse
.getEntity());
strResult = eregi_replace("(\r\n|\r|\n|\n\r)", "",
strResult);
mTextView1.setText(strResult);
} else {
mTextView1.setText("Error Response: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e) {
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
public String eregi_replace(String strFrom, String strTo, String strTarget) {
String strPattern = "(?i)" + strFrom;
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strTarget);
if (m.find()) {
return strTarget.replaceAll(strFrom, strTo);
} else {
return strTarget;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Android应用源码之http.zip项目安卓应用源码下载Android应用源码之http.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
资源推荐
资源详情
资源评论
收起资源包目录
Android应用源码之http.zip (24个子文件)
Android应用源码之http
Android应用源码之http
http
.project 840B
default.properties 449B
bin
com
test
R$string.class 467B
R$layout.class 364B
R$drawable.class 428B
R.class 461B
HttpActivity$1.class 3KB
R$attr.class 310B
HttpActivity.class 2KB
HttpActivity$2.class 3KB
R$id.class 425B
http.apk 16KB
resources.ap_ 11KB
classes.dex 6KB
AndroidManifest.xml 973B
src
com
test
HttpActivity.java 4KB
res
values
color.xml 158B
strings.xml 298B
layout
main.xml 803B
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
.classpath 280B
gen
com
test
R.java 1KB
共 24 条
- 1
资源评论
yxkfw
- 粉丝: 76
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功