我正在尝试使用jsonObject和jSONArray做一些Android简单的jsonParsing应用程序,以在模拟器上显示一些数据。

但是问题是系统强制关闭并给任何人可以帮助我吗?

原木猫

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;
    }

}

最佳答案

网址应为

http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username=devleb

的json

{
    "postalcodes": [ // postalcodes is the json array
        {
            "adminCode3": "70805",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Breitenwang",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.7333333,
            "placeName": "Breitenwang",
            "lat": 47.4833333,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70806",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Ehenbichl",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.7,
            "placeName": "Ehenbichl",
            "lat": 47.4666667,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70820",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Lechaschau",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.7,
            "placeName": "Lechaschau",
            "lat": 47.4833333,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70822",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Musau",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.6666667,
            "placeName": "Musau",
            "lat": 47.5333333,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70826",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Pflach",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.7,
            "placeName": "Oberletzen",
            "lat": 47.5166667,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70827",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Pinswang",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.6833333,
            "placeName": "Oberpinswang",
            "lat": 47.5333333,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70826",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Pflach",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.7166667,
            "placeName": "Pflach",
            "lat": 47.5166667,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70827",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Pinswang",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.6666667,
            "placeName": "Pinswang",
            "lat": 47.5333333,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70828",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Reutte",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.7166667,
            "placeName": "Reutte",
            "lat": 47.4833333,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70826",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Pflach",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.7,
            "placeName": "Unterletzen",
            "lat": 47.5166667,
            "adminName1": "Tirol"
        },
        {
            "adminCode3": "70827",
            "adminName2": "Politischer Bezirk Reutte",
            "adminName3": "Pinswang",
            "adminCode2": "708",
            "postalcode": "6600",
            "adminCode1": "07",
            "countryCode": "AT",
            "lng": 10.6666667,
            "placeName": "Unterpinswang",
            "lat": 47.5333333,
            "adminName1": "Tirol"
        }
    ]
}



你也有
private static final String POSTALCODE = "postalCodeLookup"

然后
JSONObject jsonObject = (JSONObject) new JSONTokener(JSONResponse)
                .nextValue();

JSONArray PostalCodes = jsonObject.getJSONArray(POSTALCODE);

它应该是
private static final String POSTALCODE = "postalcodes"
JSONArray PostalCodes = jsonObject.getJSONArray(POSTALCODE);

10-02 00:28