JSON Tutorial

Web Hosting
In this tutorial, I will show you how to pass and retrieve data on a webservice. Here's the sample code below. Create Activity and named it Sample.java.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

public class Sample {
public final static String ServerIP = "";
public final static String URL = "";
public Sample() 
{ }

//This part is receiving a data to pass in a web service

private JSONObject sendJsonRequest(String host, int port,String uri, JSONObject param) 
           throws ClientProtocolException, IOException, JSONException
   HttpClient httpClient = new DefaultHttpClient();
   HttpHost httpHost = new HttpHost(host, port);   
   HttpPost httpPost = new HttpPost(uri);
         //To make sure that data pass converted to json object.
   httpPost.addHeader("Content-Type", "application/json; charset=utf-8");

if (param != null)
   HttpEntity bodyEntity = new StringEntity(param.toString(), "utf8");

HttpResponse response = httpClient.execute(httpHost, httpPost);
HttpEntity entity = response.getEntity();

String result = null;
if (entity != null) {
   InputStream instream = entity.getContent();
   BufferedReader reader = new BufferedReader(
        new InputStreamReader(instream));
   StringBuilder sb = new StringBuilder();

   String line = null;
   while ((line = reader.readLine()) != null)
       sb.append(line + "\n");

   result = sb.toString();

return result != null ? new JSONObject(result) : null;


//Create a json object of your data input because the webservice needs a json object to pass.

public JSONObject Sample(String your_field1  String your_field2  String your_field3  String your_field4  String your_field5, String your_field6, String your_field7) throws Exception {
JSONObject result = null;
   JSONObject param = new JSONObject();
   param.put("samekey_api1", your_field1);
   param.put("samekey_api2", your_field2);
   param.put("samekey_api3", your_field3);
   param.put("samekey_api4", your_field4);
   param.put("samekey_api5", your_field5);
   param.put("samekey_api6", your_field6);
   param.put("samekey_api7", your_field7);

   result = sendJsonRequest(ServerIP, 80, URL, param);
   /*if (result == null)
      result = "null";
catch (Exception ex)
return result;


And in your MainActivity.java. Simple call this code below.

//instantiating the function
SendoutMobile ws = new SendoutMobile();
resp= ws.SendoutMobile(your_field1your_field2  your_field3  your_field4  your_field5  your_field6  text4.getText().toString().replaceAll(",",""));

//to retrieve the data example        
String repscode = resp.getString("respcode");

Web Hosting

No comments:

Post a Comment