我使用HTTP Get從我的網站檢索JSON格式的字符串。爲什麼我無法創建JSON對象?
但是,當我嘗試創建JSONObject時,沒有任何反應。該對象沒有創建,我不能訪問數組或其中的任何內容。
我檢查過我的應用程序沒有得到正確的字符串,並且它被正確格式化爲JSON。
我必須以某種特定的方式聲明它,或者有單獨的try/catch塊嗎?
我在我無計可施這樣結束,所以任何幫助將是非常讚賞...
下,你擁有所有我寫在這個問題上的代碼,雖然它的並不多,我卡住,不知道去哪裏這個...
public String result, testString;
public TextView resultText;
public String url = "http://www.ace.ucv.ro/android/android.php";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
resultText = (TextView) findViewById(R.id.result);
try {
HttpGet httpGet = new HttpGet(url);
result = EntityUtils.toString(new DefaultHttpClient().execute(httpGet).getEntity());
JSONObject jsonObject = new JSONObject(result);
} catch (ParseException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
resultText.setText(result);
}
你的代碼看起來不錯,你如何檢查jsonObject是否被填充? –
首先,在JSONObject之後沒有任何操作完成,比如獲取數組或值等。其次,在執行創建Object之後,沒有在該try/catch塊中寫入的指令。 (用簡單的東西測試,如result =「test」; – Eugen
看起來你正在打印堆棧跟蹤...你能分享它嗎? – Aurora