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();
}
請顯示您的logcat。 –
目前尚不清楚您收到的錯誤以及在哪裏。 – ElmerCat
而你的json無效! –