没空写文字,直接上代码,以后有空再补文字!
package com.jinbi.app.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
private String urlAPI;
private HttpClient httpClient;
public HttpUtil(String urlAPI){
this.urlAPI = urlAPI;
httpClient = new DefaultHttpClient();
}
public String getHttpMessage(String skey,String token){
String strResult = null;
try {
/*建立HTTP Post连线*/
HttpPost post = new HttpPost(urlAPI);
//Post运作传送变数必须用NameValuePair[]阵列储存
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("skey", skey));
params.add(new BasicNameValuePair("token", token));
//设置请求参数
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
//判断服务器是否成功响应
if (httpResponse.getStatusLine().getStatusCode() == 200){
//读取响应的数据
strResult = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return strResult;
}
public String getHttpMessage(String uid,String pass,String token){
String strResult = null;
try {
/*建立HTTP Post连线*/
HttpPost post = new HttpPost(urlAPI);
//Post运作传送变数必须用NameValuePair[]阵列储存
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uid", uid));
params.add(new BasicNameValuePair("pass", pass));
params.add(new BasicNameValuePair("token", token));
//设置请求参数
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
//判断服务器是否成功响应
if (httpResponse.getStatusLine().getStatusCode() == 200){
//读取响应的数据
strResult = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}
return strResult;
}
}