我从互联网上的PHP文件中获取此JSON对象:
{
"1": "bar",
"2": "foo",
"3": "umIar",
"4": "shubham"
}
我尝试了很多,但无法将其转换为String
数组。怎么做?
我使用了这个JAVA代码:
public class showNewsFeed extends AsyncTask <String, Void, String> {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.ludlowcastle.co.in/getNotifiedDTU/newsFeed.php");
String result;
protected String doInBackground(String... voids ){
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String result = convertStreamToString(is);
System.out.println("String response = " + result);
} catch (Exception e){
e.printStackTrace();
}
return result;
}
protected void onProgressUpdate(Void...voids){
}
protected void onPostExecute(String result){
super.onPostExecute(result);
try {
JSONObject obj=new JSONObject(result);
Iterator iter = obj.keys();
while(iter.hasNext()){
String key = (String)iter.next();
System.out.println(obj.getString(key));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是logcat:
02-18 04:58:19.412: W/System.err(1268): java.lang.NullPointerException
02-18 04:58:19.431: W/System.err(1268): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
02-18 04:58:19.431: W/System.err(1268): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
02-18 04:58:19.431: W/System.err(1268): at org.json.JSONObject.<init>(JSONObject.java:154)
02-18 04:58:19.431: W/System.err(1268): at org.json.JSONObject.<init>(JSONObject.java:171)
02-18 04:58:19.431: W/System.err(1268): at com.umair.shubham.fragment1$showNewsFeed.onPostExecute(fragment1.java:71)
02-18 04:58:19.431: W/System.err(1268): at com.umair.shubham.fragment1$showNewsFeed.onPostExecute(fragment1.java:1)
02-18 04:58:19.441: W/System.err(1268): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-18 04:58:19.441: W/System.err(1268): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-18 04:58:19.441: W/System.err(1268): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-18 04:58:19.451: W/System.err(1268): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 04:58:19.451: W/System.err(1268): at android.os.Looper.loop(Looper.java:137)
02-18 04:58:19.451: W/System.err(1268): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-18 04:58:19.451: W/System.err(1268): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 04:58:19.451: W/System.err(1268): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 04:58:19.451: W/System.err(1268): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-18 04:58:19.461: W/System.err(1268): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-18 04:58:19.471: W/System.err(1268): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
试试这个
ArrayList<String> arr=new ArrayList<String>();
JSONObject obj=new JSONObject(response);
Iterator iter = obj.keys();
while(iter.hasNext()){
String key = (String)iter.next();
arr.add(obj.getString(key));
}