package com.utils; java
import java.io.IOException; apache
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map; json
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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.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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; 服務器
public class TestHttp { app
public static String HttpGet(String url) throws ClientProtocolException, IOException{
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
HttpResponse httpResponse= httpClient.execute(httpGet);
post
return getResult(httpResponse);
}
public static String getResult(HttpResponse response) throws ParseException, IOException{
String result="";
//若是發送成功...
if(response.getStatusLine().getStatusCode()==200){
result=EntityUtils.toString(response.getEntity());
return result;
}else{
return "服務器響應失敗....TestHttp";
}
}
public static JSONArray JsonHttpGet(String url) throws ParseException, IOException, JSONException{
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
HttpResponse response= httpClient.execute(httpGet);
JSONArray json = null;
//若是發送成功...
if(response.getStatusLine().getStatusCode()==200){
//result=EntityUtils.toString(response.getEntity());
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
byte[] bytes = new byte[256];
StringBuffer sb = new StringBuffer();
while (is.read(bytes) > 0) {
sb.append(new String(bytes));
bytes = new byte[256];
}
//new JSONArray(sb.toString())
json = new JSONArray(sb.toString());
//json = JSONObject.fromObject(sb.toString());
url
return json;
}else{
return null;
}
}
/**
* 模擬url訪問 從特定的url中獲取json
*
* @param urlStr
* @param params
* @return json object ,or null if failed
* 參數通過封裝後傳過來 ,提交爲 post請求
*/
private static JSONObject getJsonFromUrl(String urlStr,
Map<String, String> params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(urlStr);
JSONObject json = null;
try {
if (params != null) {
Iterator<String> keys = params.keySet().iterator();
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
while (keys.hasNext()) {
String key = keys.next();
nvps.add(new BasicNameValuePair(key, params.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
}
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
byte[] bytes = new byte[256];
StringBuffer sb = new StringBuffer();
while (is.read(bytes) > 0) {
sb.append(new String(bytes));
bytes = new byte[256];
}
//json ="";//JSONObject.fromObject(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} .net
return json;
} code
} orm