-2

我想显示 URL 的 JSON,URL 是(http://loyaltier.com/app/mobile/code/places/Maps.php),我使用 GSON 并编写此代码,但是当我运行程序时,程序没有'不显示gsonFoo方法的日志。问题可能是什么?我阅读此链接以使用 GSON,https: //stackoverflow.com/questions/7939632/gson-jackson-in-android

东西.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;
  }

我的位置.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");
  }
}   
4

2 回答 2

2

您可以在不使用 Gson 的情况下粘贴 json 字符串:

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...
 }
于 2012-11-20T19:14:18.777 回答
0

Thing thing = gson.fromJson(new FileReader("http://loyaltier.com/app/mobile/code/places/Maps.php"), Thing.class);包含错误,它需要FileReader类的对象而不是 json 数据。因此,您必须从链接中获取 json 数据并将其传递给FileReader对象。根据链接中提供的json数据,

[{"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"}]

它似乎是Thing's 对象的列表,因此您必须修改ThingList类型。List将包含Things 个对象。修改后的代码如下:

东西.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;
    }

}

修改gsonFoo()方法为:

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

在方法中,String变量input包含从给定的 .json 检索到的json数据url。并将数据传递给方法以创建List. 并且List在其每个数组中都包含用于创建对象的json数据。Thing

但是从给定链接中检索到的 json 数据中存在错误。在 json 数据中,您必须删除 json 对象值中的空格。例如,"BranchName":"The Dubai Mall"在其值中包含空格,将其转换为"BranchName":"TheDubaiMall". 并删除每个 json 值中的空格。它会正常工作。尝试下面给出的示例json数据:

[{"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"}]

它会正常工作,因为它在任何json值中都不包含空格。

于 2013-01-17T11:25:46.150 回答