Create New Android Project in Eclipse.1. Name of Project: - TestJSONWebService2. Build Target: - Android (2.2)3. Applicati...
int receivedJArrayLength;       private static String url = "http://202.71.142.203:8871/Service.svc/GetProjects";        /...
listItems.add(displayString);                              } catch (JSONException e) {                                    ...
BufferedReader in = null;    // the service response    HttpResponse response = null;    try {             // call the req...
}Now our main.xml is as follows.<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android....
Upcoming SlideShare
Loading in...5
×

Jason parsing

2,064

Published on

This document is used to guide for accessing Restful JSON webservice

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,064
On Slideshare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
55
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Jason parsing"

  1. 1. Create New Android Project in Eclipse.1. Name of Project: - TestJSONWebService2. Build Target: - Android (2.2)3. Application Name: - TestWebService4. Package Name: - parallelminds.webservice.com5. Create Activity: - TestWebServiceActivityNow our First Activity is TestWebServiceActivity which as followes.This class is used to makea 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. 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. 3. listItems.add(displayString); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return listItems; }}This is our second class CallWebServicepackage 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. 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. 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>

×