2014-03-07 41 views
0

我想做一些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; 
    } 

} 
+0

發佈JSON請 – Raghunandan

+1

它是在URL中,如果你進入它會顯示網址json – user3006788

+1

@ user3006788你在我的廣告中缺少了一個'=' – Kedarnath

回答

1

URL應該

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" 
     } 
    ] 
} 

org.json.JSONException:沒有價值for postalCodeLookup

還你

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); 
+0

感謝您的幫助 – user3006788

+0

@ user3006788歡迎您的光臨。很高興我的建議工程 – Raghunandan

+0

@ Raghunandan現在,如果我想顯示和分類這些數據的領域如何做到這一點?如表或文本視圖意味着每個值將顯示在特定字段 – user3006788

0

嘗試您的網址更改爲:

私有靜態最後字符串URL = 「http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username=」 + USER_NAME;

我猜烏爾錯過了 「=」

+0

ded「=」並將用戶名更改爲「devleb」這是我的帳戶,但仍然無效 – user3006788

+0

我無法在json響應中找到任何POSTALCODE =「postalCodeLookup」。嘗試 JSONArray PostalCodes = jsonObject.getJSONArray(「postalcodes」); – ZealDeveloper

+0

請檢查您的常量,POSTALCODE應該是「postalcodes」,而不是「postalCodeLookup」根據響應。 – ZealDeveloper