Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
74 views

Android Default Json Parsor Class

This class defines methods for parsing JSON objects from URLs or HTTP requests. The getJSONFromUrl method makes an HTTP request to a given URL, reads the response as a string, and parses it into a JSON object. The makeHttpRequest method handles making either GET or POST requests, reading the response and parsing it into a JSON object. Both methods return the JSON object or throw exceptions if errors occur during the request or parsing.

Uploaded by

madhav nimavat
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Android Default Json Parsor Class

This class defines methods for parsing JSON objects from URLs or HTTP requests. The getJSONFromUrl method makes an HTTP request to a given URL, reads the response as a string, and parses it into a JSON object. The makeHttpRequest method handles making either GET or POST requests, reading the response and parsing it into a JSON object. Both methods return the JSON object or throw exceptions if errors occur during the request or parsing.

Uploaded by

madhav nimavat
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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;
}

You might also like