我一直在试图弄清楚过去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