-2

I want to show JSON of URL, URL is (http://loyaltier.com/app/mobile/code/places/Maps.php), I use GSON and write this code, but when I run the program, the program doesn't show the log of gsonFoo method. What could the problem be? I read this link for use GSON ,https://stackoverflow.com/questions/7939632/gson-jackson-in-android

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

MyLocation.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");
  }
}   
Community
  • 1
  • 1

2 Answers2

2

You can pase your json String without using Gson as:

JSONArray ja = new JSONArray("Your json String");
for (int i = 0; i < ja.length(); i++) {
  JSONObject oneObject = jArray.getJSONObject(i);
  // Pulling items from the array
   String oneBranchId = oneObject.getString("BranchId");
   String oneBranchCode = oneObject.getString("BranchCode");
   String oneBranchTel = oneObject.getString("BranchTel");
   String oneAddress = oneObject.getString("Address");
    //your code here...
 }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • What is "your json string"?Do you mean i copy URL address instead of your json string in JSONArray? – simon Quicke Nov 20 '12 at 19:50
  • not @simonQuicke you will need to use Httpget or post for getting jsonstring from server or from url and then pass this string to JSONArray and note that you must use asyncTask for getting data from server. you can see this example for getting json from url http://stackoverflow.com/questions/11579693/how-to-get-json-data-from-php-server-to-android-mobile – ρяσѕρєя K Nov 20 '12 at 19:54
0

Thing thing = gson.fromJson(new FileReader("http://loyaltier.com/app/mobile/code/places/Maps.php"), Thing.class); contains errors, it takes the object of FileReader class not a json data. So you have to take the json data from the link and pass it in the place of a FileReader object. According to the json data provided in the link,

[{"BranchId":"1","BranchCode":"b1","BranchName":"The Dubai Mall","BranchTel":"+ 971 4 339 9716","Address":"The Waterfalls, Lower Ground LG-119 Dubai Mall","CityName":"Dubai","CountryName":"UAE","Latitude":"25.197427","Longitude":"55.279251","WorkingHours":"10:00AM - 10:30PM","BranchImage":"dubai-mall.jpg"},{"BranchId":"2","BranchCode":"b2","BranchName":"The Dubai Festival City","BranchTel":"+ 971 4 232 8856","Address":"Shop 102, Ground Floor, North Oval, Dubai Festival City","CityName":"Dubai","CountryName":"UAE","Latitude":"25.22319","Longitude":"55.350394","WorkingHours":"10:00AM - 10:30PM","BranchImage":"festival-City.jpg"},{"BranchId":"3","BranchCode":"b3","BranchName":"Dubai Media City","BranchTel":"+971 4 449 4010 ","Address":"Tower B, Business Central Towers, Dubai Media City","CityName":"Dubai","CountryName":"UAE","Latitude":"25.094713","Longitude":"55.154604","WorkingHours":"10:00AM - 10:30PM","BranchImage":"media-city.jpg"}]

it seems to be a list of Thing's objects, so you have to modify Thing to a List type. The List will contain the Things objects. The modified code is as follows :

Thing.java

package org.example.loyaltier;

import java.io.Serializable;


public class Thing implements Serializable{
private String branchId;
private String branchCode;
private String branchName;
private String branchTel;
private String address;
private String cityName;
private String countryName;
private String latitude;
private String longitude;
private String workingHours;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBranchCode() {
        return branchCode;
    }

    public void setBranchCode(String branchCode) {
        this.branchCode = branchCode;
    }

    public String getBranchId() {
        return branchId;
    }

    public void setBranchId(String branchId) {
        this.branchId = branchId;
    }

    public String getBranchName() {
        return branchName;
    }

    public void setBranchName(String branchName) {
        this.branchName = branchName;
    }

    public String getBranchTel() {
        return branchTel;
    }

    public void setBranchTel(String branchTel) {
        this.branchTel = branchTel;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getWorkingHours() {
        return workingHours;
    }

    public void setWorkingHours(String workingHours) {
        this.workingHours = workingHours;
    }

}

Modified gsonFoo() method as :

public void gsonFoo() throws Exception {
        URL url = new URL("http://loyaltier.com/app/mobile/code/places/Maps.php");
        InputStream inputStream = url.openStream();
        byte[] bt = new byte[inputStream.available()];
        inputStream.read(bt);
        System.out.println(new String(bt));
        Gson gson = new Gson();
        String input = new String(bt);
        List listOfThings = gson.fromJson(input, List.class);
        //System.out.println(listOfThings.get(0).toString());

        for (int i = 0; i < listOfThings.size(); i++) {
            Thing thing = gson.fromJson(listOfThings.get(i).toString(), Thing.class);
            System.out.println(thing.getAddress());
        }
    }

In the method the String variable input contains the json data retrieved from the given url. And the data is passed to the method to create a List. And the List in its each array contains the json data to create the Thing object.

But there is an error in the json data retrieved from the given link. In the json data, you have to remove the white space in the value of json object. For instance, "BranchName":"The Dubai Mall" contains white spaces in its value, convert it as "BranchName":"TheDubaiMall". And remove the white spaces in every json value. It will work fine. Try the sample json data given below :

[{"branchId":"branchidinthing1","branchCode":"branchcodeinthing1","branchName":"branchnameinthing1","branchTel":"telinthing1","address":"addressinthing1","cityName":"citynameinthing1","countryName":"countrynameinthing1","latitude":"latitudeinthing1","longitude":"longitudeinthing1","workingHours":"workinghoursinthing1"},{"branchId":"branchidinthing2","branchCode":"branchcodeinthing2","branchName":"branchnameinthing2","branchTel":"telinthing2","address":"addressinthing2","cityName":"citynameinthing2","countryName":"countrynameinthing2","latitude":"latitudeinthing2","longitude":"longitudeinthing2","workingHours":"workinghoursinthing2"}]

it will work fine because it doesn't contain white space in any json value.

Visruth
  • 3,430
  • 35
  • 48