Saturday, 17 August 2013

JSONParser for Request & Response


Hi!

   I show you "How to handle the Request & Response for data transfer through JSON".


The JSON:

  • JSON is one of the Fastest way to transfer data, when compare with XML,
  • We can transfer even large amount of data through JSON.
  • Data type should be any one of the fallow double,boolean,Array,String,Object & null.
  • JSON mostly support for all languages.
I show you blow how to handle the Request & Response through JSON.


package dcs.raj.mylist;

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.impl.client.DefaultHttpClient;

public class JSONParser {

    // Declare InputStream for handle input & String for return the result
static InputStream ips = null;
static String jsn_rtn = "";

// empty constructor is necessary
public JSONParser() {

}
// create a method for input arguments "url & type & values"
public String GenrtHttpRequest(String url, String method,
List<NameValuePair> params) {
try {
// if the method is post
if (method == "POST") {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
ips = httpEntity.getContent();


// if the method is get

else if (method == "GET") {
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
ips = httpEntity.getContent();
}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(ips, "iso-8859-1"), 8);
//use StringBuilder to concordinate the String values
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
ips.close();
jsn_rtn = sb.toString();
} catch (Exception e) {
e.getMessage();
}
// return the final result values
return jsn_rtn;
}
}


How To Handle It!


JSONParser jsonParser = new JSONParser();

This is the part of code for AsyncTask class


protected String doInBackground(String... args) {

List<NameValuePair> parameter = new ArrayList<NameValuePair>();

 parameter .add(new BasicNameValuePair("name", username));
 parameter .add(new BasicNameValuePair("id", user_id));

String json = jsonParser.GenrtHttpRequest(URL_User,"GET", parameter);

Log.d("Use_Info", json);

try {
JSONObject jObj = new JSONObject(json);
if(jObj != null){
UserName= jObj.getString(UserName);
}

} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

Notes : 


On update of Android API 23 the Apache plugin was Deprecated, we should update the fallowing line into build.gradle file.

android {
             ....
             ....
             useLibrary 'org.apache.http.legacy'
             ... 
                }

   
You can download the ready made jar fie for the above class.

Thank You!

Please Leave Your Comment..,



Have A happy Day..,

No comments:

Post a Comment