Thursday, 4 December 2014

Pass Data through JSON with all Method

JSON POST DATA


For pass parameter and get result
 Post Method


HttpClient client = new DefaultHttpClient();
                               //Timeout Limit
                                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);                                           HttpResponse response;
HttpPost post = new HttpPost(Comman.URL+"PaymentList");
//http://easylife.gserb.org/api/PaymentList
StringEntity se = new StringEntity(jsonObj.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,                                                           "application/json"));
post.setEntity(se);
response = client.execute(post);
BufferedReader reader = new BufferedReader(new                                                                                 InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String response1 = reader.readLine();
JSONTokener tokener = new JSONTokener(response1);
JSONArray jArray = new JSONArray(tokener);

For without pass parameter and get result
Get Method


                                 HttpClient client = new DefaultHttpClient();
        //Timeout Limit
                                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
HttpGet post = new                                                                                                                                                                                                                                                                                            HttpGet(Comman.URL+"OutFlow/"+hasMap.get(SessionStore.CUSTOMER_ID));
response = client.execute(post);
Log.v("Response ", "response : "+response);
BufferedReader reader = new BufferedReader(new                                                                                 InputStreamReader(response.getEntity().getContent(), "UTF-8"));
JSONTokener tokener = new JSONTokener(reader.readLine());
JSONArray jArray = new JSONArray(tokener);


For Edit
Put Mathod


HttpClient client = new DefaultHttpClient();
//Timeout Limit
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
HttpPut put = new HttpPut(params[1]);
StringEntity se = new StringEntity(params[0]);
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
put.setEntity(se);
response = client.execute(put);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
orignalResponse = reader.readLine();
JSONTokener tokener = new JSONTokener(orignalResponse);
jsonData  = new JSONObject(tokener);




For Delete 
Delete Method


//delete.setHeader(HTTP.CONTENT_TYPE, "application/json");
HttpClient client = new DefaultHttpClient();
//Timeout Limit
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); 
HttpResponse response;
HttpDelete delete = new HttpDelete(Comman.URL+"PaymentTransaction/"+hasMap.get(SessionStore.CUSTOMER_ID)+"?PaymentID="+Delete_Id+"&UserID="+hasMap.get(SessionStore.USER_ID)+"&IPAddress="+hasMap.get(SessionStore.IPADD));
response = client.execute(delete);
Log.v("Response ", "response : "+response);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
JSONTokener tokener = new JSONTokener(reader.readLine());
jsonData = new JSONObject(tokener);






No comments:

Post a Comment