Android Default Json Parsor Class
Android Default Json Parsor Class
{
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 {