JSONParser.java
package com.example.travelguide;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is=null;
static JSONObject jObj=null;
static String json="";
//constructor
public JSONParser()
{}
public JSONObject makeHttpRequest(String url,String method,List<NameValuePair> params)
{
try{
// check for request method
if(method.equals("POST")){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
//httpPost.setEntity(new UrlEncodedFormEntity(params));
//httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//设置编码
StringEntity se;
se = new StringEntity(json.toString());
httpPost.setEntity(se);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
//Log.e("Log_tag", "POST url is "+url.toString());
}else if(method.equals("GET")){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "gb2312");
url += "?"+ paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
//Log.e("Log_tag", " GET url is"+url.toString());
}
else
{
Log.e("Log_tag", "Params Error");
}
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
} catch(ClientProtocolException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
try{
//BufferedReader reader = new BufferedReader(new InputStreamReader(
// is, "iso-8859-1"), 8);
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line + "\n");
// Log.d("log_tag", "line is :"+line+sb.toString());
}
is.close();
json = sb.toString();
// Log.e("Log_tag", "this json is "+json.toString());
} catch(Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
Log.d("json", json.toString());//李霜新加的
}
// try parse the string to a JSON object
try{
jObj = new JSONObject(json);
} catch(JSONException e) {
Log.d("Log_Debug", "this json"+json.toString());
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
/*static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);// 建立HttpPost对象
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));// 设置编码
// 发送Post,并返回一个HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();// Creates a new InputStream object of
// the entity.
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
Log.d("json", json.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}*/