GET请求
package com.laituan.net;
import java.io.IOException;
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 org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
/**
* 我的get请求方式工具类
*
* </BR> </BR>
* By:苦涩 </BR>
* 联系作者:QQ 534429149
* */
public class MyGet {
public String doGet(String url) throws ClientProtocolException, IOException{
String result = null;//我们的网络交互返回值
HttpGet myGet = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setIntParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, 5*1000);
httpClient.getParams().setIntParameter(
HttpConnectionParams.SO_TIMEOUT, 30*1000);
HttpResponse httpResponse = httpClient.execute(myGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
result = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
}
return result;
}
}
package com.laituan.thread;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import android.os.Handler;
import android.os.Message;
import com.laituan.model.Model;
import com.laituan.net.MyGet;
/**
* 网络Get请求的线程
*
* </BR> </BR> By:苦涩 </BR> 联系作者:QQ 534429149
* */
public class HttpGetThread implements Runnable {
private Handler hand;
private String url;
private MyGet myGet = new MyGet();
public HttpGetThread(Handler hand, String endParamerse) {
this.hand = hand;
// 拼接访问服务器完整的地址
url = Model.HTTPURL + endParamerse;
}
@Override
public void run() {
// 获取我们回调主ui的message
Message msg = hand.obtainMessage();
try {
String result = myGet.doGet(url);
msg.what = 200;
msg.obj = result;
} catch (ClientProtocolException e) {
msg.what = 404;
} catch (IOException e) {
msg.what = 100;
}
// 给主ui发送消息传递数据
hand.sendMessage(msg);
}
POST请求
package com.laituan.net;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import android.util.Log;
import com.laituan.model.Model;
/**
* 我的post请求方式工具类
*
* </BR> </BR>
* By:苦涩 </BR>
* 联系作者:QQ 534429149
* */
public class MyPost {
public String doPost(String url,String mycode,String value){
String result = null;
HttpResponse httpResponse = null;
HttpPost post = new HttpPost(Model.HTTPURL+url);
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT,
30000); // 超时设置
client.getParams().setIntParameter(
HttpConnectionParams.CONNECTION_TIMEOUT, 10000);// 连接超时
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//Json字符串拼接
nameValuePairs.add(new BasicNameValuePair("mycode", mycode));
nameValuePairs.add(new BasicNameValuePair("value", value));
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
httpResponse = client.execute(post);
Log.e("HTTP", "CODE" + httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
Log.e("HTTP", "result:" + result);
} else {
result = null;
}
} catch (UnsupportedEncodingException e) {
result = null;
} catch (ClientProtocolException e) {
result = null;
} catch (IOException e) {
result = null;
}
return result;
}
}
package com.laituan.thread;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.laituan.net.MyPost;
/**
* 网络Post请求的线程
* *
* </BR> </BR> By:苦涩 </BR> 联系作者:QQ 534429149
* */
public class HttpPostThread implements Runnable{
private Handler hand;
private String url;
private String mycode;
private String value;
private MyPost myGet = new MyPost();
public HttpPostThread(Handler hand,String endParamerse,String mycode,String value){
this.hand = hand;
//拼接访问服务器完整的地址
url = endParamerse;
this.mycode = mycode;
this.value = value;
}
@Override
public void run() {
//获取我们回调主ui的message
Message msg = hand.obtainMessage();
String result = myGet.doPost(url, mycode, value);
msg.what = 200;
msg.obj = result;
//给主ui发送消息传递数据
hand.sendMessage(msg);
}
}
HTTP网络连接请求
package com.test.demo.client.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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.util.EntityUtils;
import android.util.Log;
import com.laituan.model.Model;
/**
* 客户端请求
*
*/
public class HttpUtil
{
// 创建HttpClient对象
public static HttpClient httpClient = new DefaultHttpClient();
public static final String Log_URL =
Model.HTTPURL+Model.SHOPURL+"/User/login.html";
public static final String Sear_URL =
Model.HTTPURL+Model.SHOPURL+"/Search/index.html";
public static final String Res_URL =
Model.HTTPURL+Model.SHOPURL+"/User/register.html";
public static final String Per_URL =
Model.HTTPURL+Model.SHOPURL+"/center/index.html";
public static final String Logout_URL=
Model.HTTPURL+Model.SHOPURL+"/User/logout.html";
public static final String Car_URL =
Model.HTTPURL+Model.SHOPURL+"/shopcart/addItem/index.html";
public static final String Buy_URL =
Model.HTTPURL+Model.SHOPURL+"/Shopcart/order.html";
/**
*
*
* @param url 发送请求的URL
* @return 服务器响应字符串
* @throws Exception
*/
public static String getRequest(String url)
throws Exception
{
// 创建HttpGet对象。
HttpGet get = new HttpGet(url);
// 发送GET请求
HttpResponse httpResponse = httpClient.execute(get);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine()
.getStatusCode() == 200)
{
// 获取服务器响应字符串
String result = EntityUtils
.toString(httpResponse.getEntity());
return result;
}
return null;
}
/**
*
* @param url 发送请求的URL
* @param params 请求参数
* @return 服务器响应字符串
* @throws Exception
*/
public static String postRequest(String url
, Map<String ,String> rawParams)throws Exception
{
// 创建HttpPost对象。
HttpPost post = new HttpPost(url);
// 如果传递参数个数比较多的话可以对传递的参数进行封装
List<NameValuePair> params = new ArrayList<NameValuePair>();
for(String key : rawParams.keySet())
{
//封装请求参数
params.add(new BasicNameValuePair(key , rawParams.get(key)));
}
// 设置请求参数
post.setEntity(new UrlEncodedFormEntity(
params, "UTF-8"));
// 发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine()
.getStatusCode() == 200)
{
Log.e("logout",url);
// 获取服务器响应字符串
String result = EntityUtils
.toString(httpResponse.getEntity());
Log.e("result11111111",result);
return result;
}
else Log.e("posterror",url);
return null;
}
}