2012-11-20 115 views
-2

我想顯示JSON的URL,URL是(http://loyaltier.com/app/mobile/code/places/Maps.php),我使用GSON並編寫這段代碼,但是當我運行該程序時,程序不顯示日誌的gsonFoo方法。問題是什麼?我讀了這個鏈接,使用GSON,https://stackoverflow.com/questions/7939632/gson-jackson-in-android解析JSON與GSON,但我不能

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

回答

2

您可以填寫Flash您的JSON字符串,而無需使用GSON爲:

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... 
} 
+0

什麼是「你的json字符串」?你的意思是我複製的URL地址,而不是你的json字符串在JSONArray? –

+0

不是@simonQuicke,您將需要使用Httpget或發佈從服務器或從url獲取jsonstring,然後將此字符串傳遞給JSONArray,並注意您必須使用asyncTask從服務器獲取數據。你可以看到這個例子從網址獲取JSON http://stackoverflow.com/questions/11579693/how-to-get-json-data-from-php-server-to-android-mobile –

0

Thing thing = gson.fromJson(new FileReader("http://loyaltier.com/app/mobile/code/places/Maps.php"), Thing.class);包含錯誤,它需要FileReader類不是json數據。所以你必須從鏈接中取出json數據,並將其傳遞給FileReader對象。 根據在link提供的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的對象的列表,所以你必須修改Thing列表類型。 列表將包含Thing的對象。 修改後的代碼如下:

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

} 

改性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從檢索數據給出url。並將數據傳遞給該方法以創建List。並且在其每個陣列中的List包含用於創建對象的數據json

但是從給定鏈接檢索到的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值空白。