package Utils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
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.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import android.text.TextUtils;
import android.util.Log;
/**
* HttpClient与HttpURLConnection 网络请求
*
* @author android.mtj 在Android6.0版本直接删除了HttpClient类库,如果仍想使用则解决方法是
* 1、如果使用的是eclipse则在libs中加入org.apache.http.legacy.jar
* 这个jar包在:**sdk\platforms\android-23\optional目录中(需要下载android 6.0的SDK)
* 2、 如果使用的是android studio则 在相应的module下的build.gradle中加入:
*
* android { useLibrary 'org.apache.http.legacy' }
*/
public class HttpRequestUtils {
private static HttpRequestUtils mHttpUtils;
/**
* 获取实例
*
* @return
*/
public static HttpRequestUtils getInstance() {
if (mHttpUtils == null) {
mHttpUtils = new HttpRequestUtils();
}
return mHttpUtils;
};
/**
* 创建HttpGet 请求
*
* @param url
* 请求地址
*/
public String useHttpClientGet(String url) {
String result = "";
HttpGet mHttpGet = new HttpGet(url);
mHttpGet.addHeader("Connection", "Keep-Alive");
try {
HttpClient mHttpClient = createHttpClient();
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
HttpEntity mHttpEntity = mHttpResponse.getEntity();
if (null != mHttpEntity) {
InputStream mInputStream = mHttpEntity.getContent();
result = converStreamToString(mInputStream);
mInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
Log.i("mtj", "result== "+result);
return result;
}
/**
* 创建HttpPost 请求
*
* @param url
* 请求地址
* @param map
* 请求参数
*/
public String useHttpClientPost(String url, Map<String, String> map) {
String result = "";
HttpPost mHttpPost = new HttpPost(url);
mHttpPost.addHeader("Connection", "Keep-Alive");
try {
HttpClient mHttpClient = createHttpClient();
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
postParams.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
mHttpPost.setEntity(new UrlEncodedFormEntity(postParams));
HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
HttpEntity mHttpEntity = mHttpResponse.getEntity();
if (null != mHttpEntity) {
InputStream mInputStream = mHttpEntity.getContent();
result = converStreamToString(mInputStream);
mInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
Log.i("mtj", "result== "+result);
return result;
}
/**
* 创建HttpUrlConnectionPost 请求
*
* @param url
* 请求地址
* @param map
* 请求参数
* @return
*/
public String useHttpUrlConnectionPost(String url, Map<String, String> map) {
String result = "";
InputStream mInputStream = null;
HttpURLConnection mHttpURLConnection = getHttpURLConnection(url, "POST");
try {
List<NameValuePair> post = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
post.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
postParams(mHttpURLConnection.getOutputStream(), post);
mHttpURLConnection.connect();
mInputStream = mHttpURLConnection.getInputStream();
result = converStreamToString(mInputStream);
mInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("mtj", "result== "+result);
return result;
}
/**
* 创建HttpUrlConnectionGet 请求
*
* @param url
* 请求地址
* @return
*/
public String useHttpUrlConnectionGet(String url) {
String result = "";
InputStream mInputStream = null;
HttpURLConnection mHttpURLConnection = getHttpURLConnection(url, "GET");
try {
mHttpURLConnection.connect();
mInputStream = mHttpURLConnection.getInputStream();
result = converStreamToString(mInputStream);
mInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("mtj", "result== "+result);
return result;
}
private HttpClient createHttpClient() {
HttpParams mDefaultHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(mDefaultHttpParams, 15000);
HttpConnectionParams.setSoTimeout(mDefaultHttpParams, 15000);
HttpConnectionParams.setTcpNoDelay(mDefaultHttpParams, true);
HttpProtocolParams.setVersion(mDefaultHttpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(mDefaultHttpParams, HTTP.UTF_8);
HttpProtocolParams.setUseExpectContinue(mDefaultHttpParams, true);
HttpClient mHttpClient = new DefaultHttpClient(mDefaultHttpParams);
return mHttpClient;
}
private String converStreamToString(InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
String respose = sb.toString();
return respose;
}
private static HttpURLConnection getHttpURLConnection(String url,
String requestMethod) {
HttpURLConnection mHttpURLConnection = null;
try {
URL mUrl = new URL(url);
mHttpURLConnection = (HttpURLConnection) mUrl.openConnection();
mHttpURLConnection.setConnectTimeout(15000);
mHttpURLConnection.setReadTimeout(15000);
mHttpURLConnection.setRequestMethod(requestMethod);
mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive");
mHttpURLConnection.setDoInput(true);
mHttpURLConnection.setDoOutput(true);
} catch (IOException e) {
e.printStackTrace();
}
return mHttpURLConnection;
}
private void postParams(OutputStream output, List<NameValuePair> paramsList)
throws IOException {
StringBuilder mStringBuilder = new StringBuilder();
for (NameValuePair pair : paramsList) {
if (!TextUtils.isEmpty(mStringBuilder)) {
mStringBuilder.append("&");
}
mStringBuilder.append(URLEncoder.encode(pair.getName(), "UTF-8"));
mStringBuilder.append("=");
mStringBuilder.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
output, "UTF-8"));
writer.write(mStringBuilder.toString());
writer.flush();
writer.close();
}
}