httpClient的Get和Post请求

public class MainActivity extends Activity {
String key = "3ac9f31ff66b9746539472887b3799c3";
// 接口地址
String path = "http://web.juhe.cn:8080/constellation/getAll";
// 通过get请求时的接口地址
String get_path = "http://web.juhe.cn:8080/constellation/getAll?consName=狮子座&type=today&key="
+ key;
private EditText editText;


Handler handler = new Handler() {


public void handleMessage(android.os.Message msg) {


Bean bean = (Bean) msg.obj;


// textView.setText(bean.date+"\n"+bean.name+"\n"+bean.finance+"\n"+bean.luckyStone+"\n"+bean.love.get(0));


textView.setText(bean.toString());
};


};
private TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView);


}


/**
* 点击请求网络
*/
public void send(View v) {
// 获取输入框的内容
final String name = editText.getText().toString().trim();
// 如果输入框有内容就根据输入的星座名称请求网络
if (!TextUtils.isEmpty(name)) {
new Thread() {


public void run() {


// httpGet(name);
httpPost(name);
};


}.start();


} else {


Toast.makeText(MainActivity.this, "  请输入星座 ", 1).show();
}


}


/**
* GET方式请求网络
*/
private void httpGet(String name) {
// HttpClient得到HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 以get方式请求.并设置接口地址
HttpGet httpGet = new HttpGet(path + "consName=" + name
+ "&type=year&key=3ac9f31ff66b9746539472887b3799c3");
try {
// 连接网络请求数据,请求到的数据在httpResponse对象里
HttpResponse httpResponse = httpClient.execute(httpGet);
// 服务器返回的内容在HttpEntity
HttpEntity entity = httpResponse.getEntity();
// 把entity转成String
String string = EntityUtils.toString(entity);


// System.out.println(string);


Gson gson = new Gson();


Bean bean = gson.fromJson(string, Bean.class);
Message msg = Message.obtain();
msg.obj = bean;
handler.sendMessage(msg);


} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**
* POST方式请求

* @param name
*/
protected void httpPost(String name) {


HttpClient httpclient=new DefaultHttpClient();
//通过POST方式请求
HttpPost httpPost=new HttpPost(path);

//得到一个集合,这个集合里添加的是要传递给服务器的参数
List<NameValuePair> parameters=new ArrayList<NameValuePair>();
                              //consName=狮子座
                     //type=today
//以键值对的形式添加参数
parameters.add(new BasicNameValuePair("consName", name));
parameters.add(new BasicNameValuePair("type", "year"));
parameters.add(new BasicNameValuePair("key", key));


try {
//设置实体内容和编码格式
UrlEncodedFormEntity encodedFormEntity=new UrlEncodedFormEntity(parameters, "UTF-8");
//设置实体,用于传递给服务器参数
httpPost.setEntity(encodedFormEntity);
//请求网络
HttpResponse httpResponse = httpclient.execute(httpPost);

//先得到状态行,从状态行里得到状态码
if( httpResponse.getStatusLine().getStatusCode()==200){
//得到实体
HttpEntity entity = httpResponse.getEntity();

//请求到的json
String string = EntityUtils.toString(entity);

Gson gson=new Gson();
Bean bean = gson.fromJson(string, Bean.class);


Message msg=Message.obtain();
msg.obj=bean;
handler.sendMessage(msg);
}



} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
http工具类:package com.tpl.util; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; /** * */ public class HttpClientUtil { public static void main(String arg[]) throws Exception { String url = "http://xxx/project/getxxx.action"; JSONObject params= new JSONObject(); List res=new ArrayList(); JSONObject params1 = new JSONObject(); // params1.put("code", "200"); // params1.put("phone", "13240186028"); res.add(params1); params.put("result", res); String ret = doPost(url, params).toString(); System.out.println(ret); } /** httpClient的get请求方式2 * @return * @throws Exception */ public static String doGet(String url, String charset) throws Exception { /* * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:生成一个 HttpClinet 对象并设置相应的参数。 * 2:生成一个 GetMethod 对象并设置响应的参数。 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get * 方法。 4:处理响应状态码。 5:若响应正常,处理 HTTP 响应内容。 6:释放连接。 */ /* 1 生成 HttpClinet 对象并设置参数 */ HttpClient httpClient = new HttpClient(); // 设置 Http 连接超时为5秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); /* 2 生成 GetMethod 对象并设置参数 */ GetMethod getMethod = new GetMethod(url); // 设置 get 请求超时为 5 秒
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值