SlideShare a Scribd company logo
Create New Android Project in Eclipse.

1. Name of Project: - TestJSONWebService
2. Build Target: - Android (2.2)
3. Application Name: - TestWebService
4. Package Name: - parallelminds.webservice.com
5. Create Activity: - TestWebServiceActivity

Now our First Activity is TestWebServiceActivity which as followes.This class is used to make
a call JSON web service. using callWebService() method.


package parallelminds.testservice.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TestServiceActivity extends ListActivity {

       LinearLayout objLinearLayout;
       TextView tv;
int receivedJArrayLength;
       private static String url = "http://202.71.142.203:8871/Service.svc/GetProjects";

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
        this.setListAdapter(new ArrayAdapter <String>
(this,android.R.layout.simple_list_item_1,this.ParsedJson()));

       }

        // Method returning array list of JSON web-service
       private ArrayList<String> ParsedJson()
       {
               ArrayList<String> listItems = new ArrayList<String>();

               CallWebService objCallWebService = new CallWebService();

               JSONArray receivedJArray = objCallWebService.callWebService(url);
               receivedJArrayLength = receivedJArray.length();


               TextView showmsg = new TextView(this);
               showmsg.setText(msg);
               objLinearLayout.addView(showmsg);*/
               if (receivedJArray != null)

                      for (int i = 0; i < receivedJArrayLength; i++) {
                               try {

                                     String displayString = "";
                                     JSONObject jObj = receivedJArray.getJSONObject(i);

               displayString += "------------n";
               displayString += "Id :" + jObj.getString("Id") + "n";
               displayString += "Name :" + jObj.getString("Name") + "n";
               displayString += "KickOffNotes:"+jObj.getString("KickOffNotes") + "n";
               displayString += "Description :"+ jObj.getString("Description") + "n";
               displayString += "MemberCount :" + jObj.getString("MemberCount") + "n";
               displayString += "StartDate :"+ jObj.getString("StartDate") + "n";
               displayString += "DeliveryDate :"+ jObj.getString("DeliveryDate") + "n";
               displayString += "Status :" + jObj.getString("Status")+ "nn";
               displayString += "n********";
listItems.add(displayString);

                              } catch (JSONException e) {
                                      // TODO Auto-generated catch block
                                      e.printStackTrace();
                              }
                      }

              return listItems;
       }


}

This is our second class CallWebService
package parallelminds.testservice.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;

public class CallWebService {

       // This method is used to get JSONArray Object
       JSONArray callWebService(String serviceURL) {
               JSONArray jArray = null;
               // http get client
               HttpClient client = new DefaultHttpClient();
               HttpGet getRequest = new HttpGet();
               try {
                        // get the requested URI
                        getRequest.setURI(new URI(serviceURL));
               } catch (URISyntaxException e) {
                        Log.e("URISyntaxException", e.toString());
               }
               // read the response in the buffer
BufferedReader in = null;
    // the service response
    HttpResponse response = null;
    try {
             // call the requested url
             response = client.execute(getRequest);
    } catch (ClientProtocolException e) {
             Log.e("ClientProtocolException", e.toString());
    } catch (IOException e) {
             Log.e("IO exception", e.toString());
    }
    try {
             in = new BufferedReader(new InputStreamReader(response.getEntity()
                              .getContent()));
    } catch (IllegalStateException e) {
             Log.e("IllegalStateException", e.toString());
    } catch (IOException e) {
             Log.e("IO exception", e.toString());
    }
    StringBuffer buff = new StringBuffer("");
    String line = "";
    try {
             while ((line = in.readLine()) != null) {
                      buff.append(line);
             }
    } catch (IOException e) {
             Log.e("IO exception", e.toString());

    }

    try {
            in.close();
    } catch (IOException e) {
            Log.e("IO exception", e.toString());
    }
    // now we need to parse the response
    String result = buff.toString();

    try {
           jArray = new JSONArray(result);
    } catch (JSONException e) {
           Log.e("log_tag", "Error parsing data " + e.toString());
    }
    return jArray;
}
}


Now our main.xml is as follows.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical" android:layout_width="fill_parent"
       android:layout_height="fill_parent" android:id="@+id/MainLayoutPMTS">

       <ListView android:id="@id/android:list" android:layout_height="match_parent"
              android:layout_width="match_parent" ></ListView>

       <TextView android:id="@+id/webXml" android:layout_width="fill_parent"
             android:layout_height="fill_parent">
             </TextView>

</LinearLayout>

More Related Content

Jason parsing

  • 1. Create New Android Project in Eclipse. 1. Name of Project: - TestJSONWebService 2. Build Target: - Android (2.2) 3. Application Name: - TestWebService 4. Package Name: - parallelminds.webservice.com 5. Create Activity: - TestWebServiceActivity Now our First Activity is TestWebServiceActivity which as followes.This class is used to make a call JSON web service. using callWebService() method. package parallelminds.testservice.com; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.MenuItem.OnMenuItemClickListener; import android.view.View; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class TestServiceActivity extends ListActivity { LinearLayout objLinearLayout; TextView tv;
  • 2. int receivedJArrayLength; private static String url = "http://202.71.142.203:8871/Service.svc/GetProjects"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setListAdapter(new ArrayAdapter <String> (this,android.R.layout.simple_list_item_1,this.ParsedJson())); } // Method returning array list of JSON web-service private ArrayList<String> ParsedJson() { ArrayList<String> listItems = new ArrayList<String>(); CallWebService objCallWebService = new CallWebService(); JSONArray receivedJArray = objCallWebService.callWebService(url); receivedJArrayLength = receivedJArray.length(); TextView showmsg = new TextView(this); showmsg.setText(msg); objLinearLayout.addView(showmsg);*/ if (receivedJArray != null) for (int i = 0; i < receivedJArrayLength; i++) { try { String displayString = ""; JSONObject jObj = receivedJArray.getJSONObject(i); displayString += "------------n"; displayString += "Id :" + jObj.getString("Id") + "n"; displayString += "Name :" + jObj.getString("Name") + "n"; displayString += "KickOffNotes:"+jObj.getString("KickOffNotes") + "n"; displayString += "Description :"+ jObj.getString("Description") + "n"; displayString += "MemberCount :" + jObj.getString("MemberCount") + "n"; displayString += "StartDate :"+ jObj.getString("StartDate") + "n"; displayString += "DeliveryDate :"+ jObj.getString("DeliveryDate") + "n"; displayString += "Status :" + jObj.getString("Status")+ "nn"; displayString += "n********";
  • 3. listItems.add(displayString); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return listItems; } } This is our second class CallWebService package parallelminds.testservice.com; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import android.util.Log; public class CallWebService { // This method is used to get JSONArray Object JSONArray callWebService(String serviceURL) { JSONArray jArray = null; // http get client HttpClient client = new DefaultHttpClient(); HttpGet getRequest = new HttpGet(); try { // get the requested URI getRequest.setURI(new URI(serviceURL)); } catch (URISyntaxException e) { Log.e("URISyntaxException", e.toString()); } // read the response in the buffer
  • 4. BufferedReader in = null; // the service response HttpResponse response = null; try { // call the requested url response = client.execute(getRequest); } catch (ClientProtocolException e) { Log.e("ClientProtocolException", e.toString()); } catch (IOException e) { Log.e("IO exception", e.toString()); } try { in = new BufferedReader(new InputStreamReader(response.getEntity() .getContent())); } catch (IllegalStateException e) { Log.e("IllegalStateException", e.toString()); } catch (IOException e) { Log.e("IO exception", e.toString()); } StringBuffer buff = new StringBuffer(""); String line = ""; try { while ((line = in.readLine()) != null) { buff.append(line); } } catch (IOException e) { Log.e("IO exception", e.toString()); } try { in.close(); } catch (IOException e) { Log.e("IO exception", e.toString()); } // now we need to parse the response String result = buff.toString(); try { jArray = new JSONArray(result); } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } return jArray; }
  • 5. } Now our main.xml is as follows. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/MainLayoutPMTS"> <ListView android:id="@id/android:list" android:layout_height="match_parent" android:layout_width="match_parent" ></ListView> <TextView android:id="@+id/webXml" android:layout_width="fill_parent" android:layout_height="fill_parent"> </TextView> </LinearLayout>