public class JSONParser
{
static InputStream is = null;
static JSONObject jsonObj ;
static String json = ""; // default no argument constructor for jsonpase
r class
public JSONParser()
{ }
public JSONObject getJSONFromUrl(final String url)
{
// Making HTTP request
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// Executing POST request & storing the response from se
rver locally.
HttpResponse httpResponse = httpClient.execute(httpPost)
;
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) { e.printStackTrace();
}
catch (ClientProtocolException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
try {
// Create a BufferedReader
BufferedReader reader = new BufferedReader(new InputStre
amReader( is, "iso-8859-1"), 8);
// Declaring string builder
StringBuilder str = new StringBuilder();
// string to store the JSON object.
String strLine = null;
// Building while we have string !equal null.
while ((strLine = reader.readLine()) != null)
{ str.append(strLine + "\n"); }
// Close inputstream.
is.close();
// string builder data conversion to string.
json = str.toString();
} catch (Exception e)
{ Log.e("Error", " something wrong with converting resul
t " + e.toString()); }
// Try block used for pasrseing String to a json object
try {
jsonObj = new JSONObject(json);
} catch (JSONException e)
{ Log.e("json Parsering", "" + e.toString()); }
// Returning json Object.
return jsonObj;
}
public JSONObject makeHttpRequest(String url, String method, List<NameVa
luePair> params)
{
// Make HTTP request
try {
// checking request method
if(method == "POST")
{
// now defaultHttpClient object
DefaultHttpClient httpClient = new DefaultHttpCl
ient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(para
ms));
HttpResponse httpResponse = httpClient.execute(h
ttpPost);
HttpEntity httpEntity = httpResponse.getEntity()
;
is = httpEntity.getContent();
}
else if(method == "GET")
{
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpCl
ient();
String paramString = URLEncodedUtils.format(para
ms, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(h
ttpGet);
HttpEntity httpEntity = httpResponse.getEntity()
;
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e)
{ e.printStackTrace(); }
catch (ClientProtocolException e)
{ e.printStackTrace(); }
catch (IOException e)
{ e.printStackTrace(); }
try {
BufferedReader reader = new BufferedReader(new InputStre
amReader( is, "iso-8859-1"), 8);
StringBuilder str = new StringBuilder();
String strLine = null;
while ((strLine = reader.readLine()) != null)
{
str.append(strLine + "\n");
}
is.close();
json = str.toString();
} catch (Exception e)
{ }
// now will try to parse the string into JSON object
try
{
jsonObj = new JSONObject(json);
}
catch (JSONException e)
{ }
return jsonObj;
}