我已經成功運行了類似的代碼,但這次它不工作!我真的不明白爲什麼。一切看起來不錯,但JSON在Android中返回空
任何想法?
public class SingleMenuItemActivity extends Activity {
private static String url = "http://www.tatangalar.com/android/takim_detay.php?takimID=5";
private static final String TAG_MACBILGISI = "son10mac";
private static final String TAG_10 = "10";
private static final String TAG_9 = "9";
JSONArray sonuclar = null;
String filan;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.benim2);
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
sonuclar = json.getJSONArray(TAG_MACBILGISI);
for(int i = 0; i < sonuclar.length(); i++){
JSONObject c = sonuclar.getJSONObject(i);
String s_on = c.getString(TAG_10);
String s_dokuz = c.getString(TAG_9);
filan = s_on;
}
} catch (JSONException e) {
e.printStackTrace();
}
TextView txtsecilen = (TextView) findViewById(R.id.son9);
txtsecilen.setText(filan);
}
}
請調試您的項目並觀察JSONObject json是否有從url返回的任何值。 – 2013-04-27 16:59:09
和你得到的錯誤是什麼,你可以粘貼錯誤堆棧跟蹤。 – 2013-04-27 17:01:38
這個鏈接將引導你:http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – 2013-04-27 17:02:04