-3

I am using a web service that returns JSON in response. I want to parse the response and crate a list of object. I Don't know how to show the parsed data in UI in android. where user can select one item form the list (i.e. Dubai mall or dubai media city or dubai festival).

public class MyLocation extends MapActivity {
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);

    initButtons();

    initMapView();
    initMyLocation();   
                    jObject=getJSONFile("http://loyaltier.com/app/mobile/code/places/Maps.php");
}
    private JSONObject getJSONFile(String URL) {
    JSONObject jObject=null;
    HttpGet httpGet=new HttpGet(URL); //http://loyaltier.com/app/mobile/code/places/Maps.php
    HttpClient httpClient=new DefaultHttpClient();
    HttpResponse httpResponse;
    StringBuilder stringBuilder=new StringBuilder();
    try{
        httpResponse=httpClient.execute(httpGet);
        HttpEntity httpEntity=httpResponse.getEntity();
        InputStream inputStream=httpEntity.getContent();
        int c;
        while((c=inputStream.read())!=-1){
            stringBuilder.append((char)c);
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    try{
        jObject=new JSONObject(stringBuilder.toString());
    }catch(JSONException e){
        Log.e("Log_tags ", "Error parsing data "+e.toString());
    }
    return jObject;
} 
 }  
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44

4 Answers4

1

You can use jObject.getString(name); It returns the value mapped by name

Dennis Mathew
  • 408
  • 5
  • 14
0

http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php I have use this code. Its pretty easy and straight-forward. You have to change the json format a little.

ratulalahy
  • 460
  • 2
  • 8
  • 16
0

you can use the JACKSON lib for parsing the response data. Following post has some example for parsing:

GSON/Jackson in Android

http://w2davids.wordpress.com/android-json-parsing-made-easy-using-jackson/

Then you can use the List View to display the list of branch object. check this out for example.

Community
  • 1
  • 1
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
0

I write with GSON but the program not show log.i in gsonFoo method. Why? what is the problem?

Thing.java:

package org.example.loyaltier;

public class Thing {
     String branchId=null;
     String branchCode=null;
     String branchName=null;
     String branchTel=null;
     String address=null;
     String cityName=null;
     String countryName=null;
     String latitude=null;
     String longitude=null;
     String workingHours=null;
}

MainActivity.java:

public class MyLocation extends MapActivity {
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.location);
         try {
             Log.i("THISSSSSSSSS", "ISSSSSSSSS");
             Log.i("FFOOOORRRR", "YYOUUUUUUUUU");
             gsonFoo();
          } catch (Exception e1) {
         Log.i("catch", "catch");
        // TODO Auto-generated catch block
        e1.printStackTrace();
   }
 }
 public void gsonFoo() throws Exception
 {
     Gson gson = new Gson();
     Thing thing = gson.fromJson(new                     FileReader("http://loyaltier.com/app/mobile/code/places/Maps.php"), Thing.class);
System.out.println(gson.toJson(thing));
    Log.i("GSOOOON", "FOOOO");
   }
 }