package com.itrackstar.tcube.common;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.util.Log;
public class NetTool {
public NetTool() {
super();
}
/**
* 以get方式请求服务器,并且得到返回
*/
public String getJsonStringGet(String uri) throws Exception{
String result=null;
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(6 * 1000);//设置连接超时
Log.i("msg", conn.getResponseCode()+"???????");
if (conn.getResponseCode() == 200){
Log.i("msg", "成功");
InputStream is = conn.getInputStream();//得到网络返回的输入流
result = readData(is, "UTF-8");
}else{
Log.i("msg", "失败");
result="";
}
conn.disconnect();
return result;
}
/**
* post
* @param urlStr
* @param params
* @return
* @throws Exception
*/
public String getJsonStringPost(String urlStr, List<NameValuePair> params)
throws Exception {
String result = null;
HttpPost httpRequest = new HttpPost(urlStr);
httpRequest.setEntity((HttpEntity) new UrlEncodedFormEntity(params,
HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
Log.i("msg", httpResponse.getStatusLine().getStatusCode()+"");
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity());
} else {
result = "5";
}
return result;
}
private String readData(InputStream inSream, String charsetName) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while( (len = inSream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inSream.close();
return new String(data, charsetName);
}
}
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.util.Log;
public class NetTool {
public NetTool() {
super();
}
/**
* 以get方式请求服务器,并且得到返回
*/
public String getJsonStringGet(String uri) throws Exception{
String result=null;
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(6 * 1000);//设置连接超时
Log.i("msg", conn.getResponseCode()+"???????");
if (conn.getResponseCode() == 200){
Log.i("msg", "成功");
InputStream is = conn.getInputStream();//得到网络返回的输入流
result = readData(is, "UTF-8");
}else{
Log.i("msg", "失败");
result="";
}
conn.disconnect();
return result;
}
/**
* post
* @param urlStr
* @param params
* @return
* @throws Exception
*/
public String getJsonStringPost(String urlStr, List<NameValuePair> params)
throws Exception {
String result = null;
HttpPost httpRequest = new HttpPost(urlStr);
httpRequest.setEntity((HttpEntity) new UrlEncodedFormEntity(params,
HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
Log.i("msg", httpResponse.getStatusLine().getStatusCode()+"");
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity());
} else {
result = "5";
}
return result;
}
private String readData(InputStream inSream, String charsetName) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while( (len = inSream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
outStream.close();
inSream.close();
return new String(data, charsetName);
}
}