Jason parsing
Upcoming SlideShare
Loading in...5
×
 

Jason parsing

on

  • 2,377 views

This document is used to guide for accessing Restful JSON webservice

This document is used to guide for accessing Restful JSON webservice

Statistics

Views

Total Views
2,377
Slideshare-icon Views on SlideShare
2,258
Embed Views
119

Actions

Likes
1
Downloads
53
Comments
0

19 Embeds 119

http://androidminders.blogspot.com 51
http://androidminders.blogspot.in 32
http://webcache.googleusercontent.com 4
http://androidminders.blogspot.ca 3
http://androidminders.blogspot.nl 3
http://androidminders.blogspot.com.es 3
http://androidminders.blogspot.kr 3
http://androidminders.blogspot.tw 3
http://androidminders.blogspot.de 3
http://androidminders.blogspot.co.uk 2
http://androidminders.blogspot.co.il 2
http://androidminders.blogspot.it 2
http://androidminders.blogspot.mx 2
http://androidminders.blogspot.com.br 1
http://androidminders.blogspot.fr 1
http://androidminders.blogspot.sg 1
http://translate.googleusercontent.com 1
http://androidminders.blogspot.hk 1
http://androidminders.blogspot.cz 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Jason parsing Jason parsing Document Transcript

    • 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;
    • 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 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
    • 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>