我想做一些android簡單的jsonParsing應用程序使用jsonObject和jSONArray在模擬器上顯示一些數據。Android和jsonParsing使用API不起作用
但問題是,系統強制關閉,並給任何人都可以幫助我????
登錄貓
03-07 08:57:58.781: W/EGL_genymotion(3757): eglSurfaceAttrib not implemented
03-07 08:58:00.133: W/System.err(3757): org.json.JSONException: No value for postalCodeLookup
03-07 08:58:00.133: W/System.err(3757): at org.json.JSONObject.get(JSONObject.java:354)
03-07 08:58:00.133: W/System.err(3757): at org.json.JSONObject.getJSONArray(JSONObject.java:548)
03-07 08:58:00.133: W/System.err(3757): at com.devleb.jsonparsingactivitydemo.JSONParserHandler.handleResponse(JSONParserHandler.java:39)
03-07 08:58:00.133: W/System.err(3757): at com.devleb.jsonparsingactivitydemo.JSONParserHandler.handleResponse(JSONParserHandler.java:1)
03-07 08:58:00.133: W/System.err(3757): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
03-07 08:58:00.133: W/System.err(3757): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
03-07 08:58:00.133: W/System.err(3757): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
03-07 08:58:00.149: W/System.err(3757): at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:273)
03-07 08:58:00.149: W/System.err(3757): at com.devleb.jsonparsingactivitydemo.JsonActivityHttpClient$HTTPGetTask.doInBackground(JsonActivityHttpClient.java:43)
03-07 08:58:00.149: W/System.err(3757): at com.devleb.jsonparsingactivitydemo.JsonActivityHttpClient$HTTPGetTask.doInBackground(JsonActivityHttpClient.java:1)
03-07 08:58:00.149: W/System.err(3757): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-07 08:58:00.149: W/System.err(3757): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-07 08:58:00.149: W/System.err(3757): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-07 08:58:00.149: W/System.err(3757): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-07 08:58:00.149: W/System.err(3757): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-07 08:58:00.149: W/System.err(3757): at java.lang.Thread.run(Thread.java:841)
MainActivity.java
package course.examples.Networking.AndroidHttpClientJSON;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button loadButton = (Button) findViewById(R.id.button1);
loadButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
NetworkingAndroidHttpClientJSONActivity.class));
}
});
}
}
JSONParserHandler.java
package com.devleb.jsonparsingactivitydemo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
public class JSONParserHandler implements ResponseHandler<List<String>> {
private static final String PLACE_NAME_TAG = "placeName";
private static final String LONGITUDE_TAG = "lng";
private static final String LATITUDE_TAG = "lat";
private static final String ADMIN_NAME_TAG = "adminCode3";
private static final String POSTAL_CODE_TAG = "postalcode";
private static final String POSTALCODE = "postalCodeLookup";
@Override
public List<String> handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
List<String> result = new ArrayList<String>();
String JSONResponse = new BasicResponseHandler()
.handleResponse(response);
try {
JSONObject jsonObject = (JSONObject) new JSONTokener(JSONResponse)
.nextValue();
JSONArray PostalCodes = jsonObject.getJSONArray(POSTALCODE);
for (int i = 0; i < PostalCodes.length(); i++) {
JSONObject postalCode = (JSONObject) PostalCodes.get(i);
result.add(PLACE_NAME_TAG + ":"
+ postalCode.get(PLACE_NAME_TAG) + ","
+ POSTAL_CODE_TAG + ":"
+ postalCode.get(POSTAL_CODE_TAG)
+ LATITUDE_TAG
+ ":" + postalCode.get(LATITUDE_TAG) + ","
+ LONGITUDE_TAG + ":" + postalCode.get(LONGITUDE_TAG)
+ "," + ADMIN_NAME_TAG + ":"
+ postalCode.get(ADMIN_NAME_TAG) + ","
);
}
} catch (JSONException E) {
E.printStackTrace();
}
return result;
}
}
JsonActivityHttpClient.java
package com.devleb.jsonparsingactivitydemo;
import java.io.IOException;
import java.util.List;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import android.app.ListActivity;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
public class JsonActivityHttpClient extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new HTTPGetTask().execute();
}
private class HTTPGetTask extends AsyncTask<Void, Void, List<String>> {
private static final String USER_NAME = "devleb";
private static final String URL = "http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username"
+ USER_NAME;
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
@Override
protected List<String> doInBackground(Void... arg0) {
// TODO Auto-generated method stub
HttpGet request = new HttpGet(URL);
JSONParserHandler responseHandler = new JSONParserHandler();
try {
return mClient.execute(request, responseHandler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<String> result) {
// TODO Auto-generated method stub
if (null != mClient) {
mClient.close();
setListAdapter(new ArrayAdapter<String>(
JsonActivityHttpClient.this, R.layout.list_item, result));
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.json_activity_http_client, menu);
return true;
}
}
發佈JSON請 – Raghunandan
它是在URL中,如果你進入它會顯示網址json – user3006788
@ user3006788你在我的廣告中缺少了一個'=' – Kedarnath