package cn.eoe.http.get.post;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener
{
private EditText etBookName;
@Override
public void onClick(View view)
{
// ������Ҫ�������е�IP�����Լ�������IP
String url = "http://192.168.17.105:8080/querybooks/QueryServlet";
TextView tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
HttpResponse httpResponse = null;
try
{
switch (view.getId())
{
// �ύHTTP GET����
case R.id.btnGetQuery:
// ��url����������
url += "?bookname=" + etBookName.getText().toString();
// ��1��������HttpGet����
//HttpGet httpGet = new HttpGet(url);
HttpGet httpGet = new HttpGet("http://www.nb1r1y.com/dataList/visitorCount.data");
// ��2����ʹ��execute��������HTTP GET��������HttpResponse����
httpResponse = new DefaultHttpClient().execute(httpGet);
// �ж�������Ӧ״̬�룬״̬��Ϊ200��ʾ����˳ɹ���Ӧ�˿ͻ��˵�����
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
// ��3����ʹ��getEntity������÷��ؽ��
String result = EntityUtils.toString(httpResponse
.getEntity());
// ȥ�����ؽ���еġ�\r���ַ���������ڽ���ַ���������ʾһ��С����
tvQueryResult.setText(result.replaceAll("\r", ""));
}
break;
// �ύHTTP POST����
case R.id.btnPostQuery:
// ��1��������HttpPost����
HttpPost httpPost = new HttpPost(url);
// ����HTTP POST�������������NameValuePair����
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bookname", etBookName
.getText().toString()));
// ����HTTP POST�������
httpPost.setEntity(new UrlEncodedFormEntity(params,
HTTP.UTF_8));
// ��2����ʹ��execute��������HTTP POST��������HttpResponse����
httpResponse = new DefaultHttpClient().execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
// ��3����ʹ��getEntity������÷��ؽ��
String result = EntityUtils.toString(httpResponse
.getEntity());
// ȥ�����ؽ���еġ�\r���ַ���������ڽ���ַ���������ʾһ��С����
tvQueryResult.setText(result.replaceAll("\r", ""));
}
break;
}
}
catch (Exception e)
{
tvQueryResult.setText(e.getMessage());
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnGetQuery = (Button) findViewById(R.id.btnGetQuery);
Button btnPostQuery = (Button) findViewById(R.id.btnPostQuery);
etBookName = (EditText) findViewById(R.id.etBookName);
etBookName.setText("����");
btnGetQuery.setOnClickListener(this);
btnPostQuery.setOnClickListener(this);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android的HttpGetPost请求
共20个文件
class:8个
xml:4个
java:3个
需积分: 10 10 下载量 158 浏览量
2017-09-14
14:28:55
上传
评论
收藏 19KB ZIP 举报
温馨提示
简单的HttpGetPost,适合初学者,有post 和get请求的两个方式,试用简单,导入项目只需要修改请求地址就ok了。
资源推荐
资源详情
资源评论
收起资源包目录
HttpGetPost.zip (20个子文件)
HttpGetPost
bin
res
classes
cn
eoe
http
get
post
BuildConfig.class 349B
R$layout.class 400B
R$id.class 502B
R$drawable.class 406B
R$attr.class 346B
R.class 545B
Main.class 4KB
R$string.class 433B
AndroidManifest.xml 647B
res
drawable-ldpi
drawable-hdpi
drawable-xhdpi
drawable
icon.png 3KB
values
strings.xml 168B
drawable-mdpi
layout
main.xml 1KB
assets
gen
cn
eoe
http
get
post
BuildConfig.java 162B
R.java 915B
.settings
org.eclipse.jdt.core.prefs 177B
src
cn
eoe
http
get
post
Main.java 4KB
.project 814B
.classpath 475B
project.properties 648B
AndroidManifest.xml 647B
共 20 条
- 1
资源评论
haicheng20
- 粉丝: 2
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功