I want to parse REST Webservices with the help of JSON. Plz help me by giving sample code how to parse the rest webservices xml in android through JSON
2 Answers
Here is an example of JSON parsing if it is what you need, for parsing a JSON file containing contacts :
List<Contact> result = new ArrayList<Contact>();
try {
// Creation a the http client to connect to the url and get the json
// file
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlContacts);
HttpResponse httpResponse = httpclient.execute(httppost);
// json file returned as a string
jStringContacts = EntityUtils.toString(httpResponse.getEntity());
jsonContacts = new JSONObject(jStringContacts);
JSONObject contactsObject = jsonContacts.getJSONObject("contacts");
JSONArray jsonContactsArray = contactsObject.getJSONArray("contact");
// loop to extract one contact from the JSON Array
for (int i = 0; i < jsonContactsArray.length(); i++) {
jsonContact = jsonContactsArray.getJSONObject(i);
Contact contact = new Contact();
contact.firstname = jsonContact.getString("firstname");
contact.lastname = jsonContact.getString("lastname");
contact.iconName = jsonContact.getString("profileImageName");
contact.isZenika = "1".equals(jsonContact.getString("zenika"));
contact.biography = jsonContact.getString("shortBio");
result.add(contact);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
Comments
I have just now imlement....
Step1:Create A main class activity..
class main{ ///call method parse(); ParserHelper ob=new ParseHelper(this,Arraylist from parse()) }
public ArrayList<String> parse(){
ArrayList<String> listItems = new ArrayList<String>();
try{
URL twitter = new URL("http://twitter.com/statuses/public_timeline.json");
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++)
{
JSONObject jo = (JSONObject) ja.get(i);
if(jo.getString("text")!=null)
listItems.add(jo.getString("text"));
}
}
} catch (MalformedURLException e)
{
e.printStackTrace();
}
Step:2 Create class to inflate it to Main list public class JsonHelper extends BaseAdapter{
Activity a;
ArrayList<String> list=new ArrayList<String>();
private static LayoutInflater inflate=null;
JsonHelper(Activity a,ArrayList<String> list){
this.a=a;
this.list=list;
inflate=(LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
list.size();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
TextView txt1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
ViewHolder vh;
if(convertView==null){
v=inflate.inflate(R.layout.simplelistitem,null);
vh=new ViewHolder();
vh.txt1=(TextView) v.findViewById(R.id.text_view);
v.setTag(vh);
}
else{
vh=(ViewHolder) v.getTag();
}
vh.txt1.setText(list.get(position).toString());
return v;
}
}