我一直在試圖弄清楚過去4天,我即將拉出我的頭髮!無法解析JSON在我的應用程序
我需要你的幫助,任何人都可以告訴我我做錯了什麼?
這裏是JSON格式的鏈接我使用的發展:取消了隱私擔憂
這裏是我的代碼:
public class JSONActivity extends Activity {
TextView http;
HttpClient client;
JSONObject json;
final static String URL = "REMOVED FOR PRIVACY CONCERNS
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
http = (TextView) findViewById(R.id.http);
client = new DefaultHttpClient();
new Read().execute("firstName");
}
public JSONObject getpw(String password)
throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(password);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONArray getname = new JSONArray(data);
JSONObject last = getname.getJSONObject(3);
return last;
} else {
Toast.makeText(JSONActivity.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json=getpw("trustme");
return json.getString(arg0[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
http.setText(result);
}
}}
任何幫助將不勝感激。
這將是描述你的問題是非常有幫助的...... – andreapier 2012-03-08 17:55:03
我的問題是,應用程序無法檢索「firstName」,我可以看到有一些數據交換正在進行,但我的應用程序不會在最後顯示任何內容。 – 2012-03-08 18:00:08
將日誌放入您的應用中以查看發生了什麼。 「Log.d(」YourTag「,」Test hit this line「);' – Blundell 2012-03-08 18:36:34