2015-11-28 43 views
0

URL JSON解析錯誤。請檢查我的代碼。Android中令人困惑的JSON對象和JSON數組

{ 
    "info": "Central Bank of Myanmar", 
    "description": "Official Website of Central Bank of Myanmar", 
    "timestamp": "1448611200", 
    "rates": 
{ 

    "USD": "1,300.0", 
    "CZK": "51.055", 
    "JPY": "1,060.2", 
    } 

} 

活動代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.json_object); 

    TextView output = (TextView) findViewById(R.id.textView1); 

    String data = ""; 
    try { 
     String ka =callURL("http://forex.cbm.gov.mm/api/latest"); 

     JSONObject object = new JSONObject(ka); 
     JSONObject servicedata = object.getJSONObject("rates"); 
     String USD = servicedata.getString("USD"); 

     data += "USD Currency " + USD +" "; 
     output.setText(data); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

通話網址給我的錯誤。 。

public static String callURL(String myURL) { 
    System.out.println("Requeted URL:" + myURL); 
    StringBuilder sb = new StringBuilder(); 
    URLConnection urlConn = null; 
    InputStreamReader in = null; 
    try { 
     URL url = new URL(myURL); 
     urlConn = url.openConnection(); 
     if (urlConn != null) 
      urlConn.setReadTimeout(60 * 1000); 
     if (urlConn != null && urlConn.getInputStream() != null) { 
      in = new InputStreamReader(urlConn.getInputStream(), 
        Charset.defaultCharset()); 
      BufferedReader bufferedReader = new BufferedReader(in); 
      if (bufferedReader != null) { 
       int cp; 
       while ((cp = bufferedReader.read()) != -1) { 
        sb.append((char) cp); 
       } 
       bufferedReader.close(); 
      } 
     } 
     in.close(); 
    } catch (Exception e) { 
     throw new RuntimeException("Exception while calling URL:"+ myURL, e); 
    } 

    return sb.toString(); 
} 
+0

請顯示您的logcat。 –

+0

目前尚不清楚您收到的錯誤以及在哪裏。 – ElmerCat

+0

而你的json無效! –

回答

0

如果(android.os.Build.VERSION.SDK_INT> 9){ StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建立(); StrictMode.setThreadPolicy(policy); }

我把這個代碼的setContentView和代碼它的工作下。感謝您的廣告。 我花了大約一天的時間。

與Android volly庫
0

嘗試是由谷歌開發的。 你可以很容易地將json轉換爲java,反之亦然。

+0

最近我配置了錯誤。我會再試一次。 –