-1

我想从此 json url 检索数据,但给出了错误。我放了最后一个我有另一个json,这个代码可以完美地工作,但是在这里不起作用,那么问题是什么以及如何解决?

http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restaurant&rsz=8&sll=-27.5595451,-48.6206452&radius=1000&output=json

JsonParserSearch.java

 public class JsonParserSearch 
 {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JsonParserSearch() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();          

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser finaal", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

}

SearchListActivity.java

公共类 SearchlistActivity 扩展 Activity {

private static String url="http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restaurant&rsz=8&sll=-27.5595451,-48.6206452&radius=1000&output=json";
private static final String TAG_RESULTS = "results";
private static final String TAG_RESPONSEDATA="responseData";    

@Override
public void onCreate(Bundle savedInstanceState)

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    //JSONArray results = null;       
     JSONArray responseData=null;
 // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

    // Creating JSON Parser instance
    JsonParserSearch jParser = new JsonParserSearch();


    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts

         responseData = json.getJSONArray(TAG_RESPONSEDATA);
         Log.e("jsonnn data","sfsssss00000-------->"+responseData);

        }
    catch (JSONException e) {
        e.printStackTrace();
    }

}

}

11-09 01:18:14.241: E/Buffer Error(275): Error converting result java.lang.NullPointerException
11-09 01:18:14.241: E/JSON Parser finaal(275): Error parsing data org.json.JSONException: End of input at character 0 of 
11-09 01:18:14.251: D/AndroidRuntime(275): Shutting down VM
11-09 01:18:14.251: W/dalvikvm(275): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-09 01:18:14.261: E/AndroidRuntime(275): FATAL EXCEPTION: main
11-09 01:18:14.261: E/AndroidRuntime(275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ssss/com.example.ssss.SearchlistActivity}: java.lang.NullPointerException
11-09 01:18:14.261: E/AndroidRuntime(275):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-09 01:18:14.261: E/AndroidRuntime(275):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-09 01:18:14.261: E/AndroidRuntime(275):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-09 01:18:14.261: E/AndroidRuntime(275):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-09 01:18:14.261: E/AndroidRuntime(275):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 01:18:14.261: E/AndroidRuntime(275):  at android.os.Looper.loop(Looper.java:123)
11-09 01:18:14.261: E/AndroidRuntime(275):  at android.app.ActivityThread.main(ActivityThread.java:4627)
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
11-09 00:45:17.072: W/System.err(302):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-09 00:45:17.072: W/System.err(302):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-09 00:45:17.072: W/System.err(302):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-09 00:45:17.072: W/System.err(302):  at com.example.sss.JsonParserSearch.getJSONFromUrl(JsonParserSearch.java:38)
11-09 00:45:17.072: W/System.err(302):  at com.example.sss.SearchlistActivity.onCreate(SearchlistActivity.java:39)
11-09 00:45:17.072: W/System.err(302):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-09 00:45:17.082: W/System.err(302):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-09 00:45:17.082: W/System.err(302):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-09 00:45:17.082: W/System.err(302):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-09 00:45:17.082: W/System.err(302):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-09 00:45:17.082: W/System.err(302):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 00:45:17.082: W/System.err(302):  at android.os.Looper.loop(Looper.java:123)
11-09 00:45:17.082: W/System.err(302):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 00:45:17.082: W/System.err(302):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 00:45:17.082: W/System.err(302):  at java.lang.reflect.Method.invoke(Method.java:521)
11-09 00:45:17.092: W/System.err(302):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-09 00:45:17.092: W/System.err(302):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-09 00:45:17.092: W/System.err(302):  at dalvik.system.NativeStart.main(Native Method)
11-09 00:45:17.092: E/Buffer Error(302): Error converting result java.lang.NullPointerException
11-09 00:45:17.092: E/JSON Parser finaal(302): Error parsing data org.json.JSONException: End of input at character 0 of 
11-09 00:45:17.103: D/AndroidRuntime(302): Shutting down VM
11-09 00:45:17.103: W/dalvikvm(302): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-09 00:45:17.112: E/AndroidRuntime(302): FATAL EXCEPTION: main
11-09 00:45:17.112: E/AndroidRuntime(302): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sss/com.example.sss.SearchlistActivity}: java.lang.NullPointerException
11-09 00:45:17.112: E/AndroidRuntime(302):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-09 00:45:17.112: E/AndroidRuntime(302):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-09 00:45:17.112: E/AndroidRuntime(302):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-09 00:45:17.112: E/AndroidRuntime(302):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-09 00:45:17.112: E/AndroidRuntime(302):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 00:45:17.112: E/AndroidRuntime(302):  at android.os.Looper.loop(Looper.java:123)
11-09 00:45:17.112: E/AndroidRuntime(302):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-09 00:45:17.112: E/AndroidRuntime(302):  at java.lang.reflect.Method.invokeNative(Native Method)
11-09 00:45:17.112: E/AndroidRuntime(302):  at java.lang.reflect.Method.invoke(Method.java:521)
11-09 00:45:17.112: E/AndroidRuntime(302):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-09 00:45:17.112: E/AndroidRuntime(302):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-09 00:45:17.112: E/AndroidRuntime(302):  at dalvik.system.NativeStart.main(Native Method)
11-09 00:45:17.112: E/AndroidRuntime(302): Caused by: java.lang.NullPointerException
11-09 00:45:17.112: E/AndroidRuntime(302):  at com.example.sss.SearchlistActivity.onCreate(SearchlistActivity.java:44)
11-09 00:45:17.112: E/AndroidRuntime(302):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-09 00:45:17.112: E/AndroidRuntime(302):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-09 00:45:17.112: E/AndroidRuntime(302):  ... 11 more
4

1 回答 1

0

您的手机/模拟器似乎无法解析 ajax.googleapis.com 的主机名。(java.net.UnknownHostException)。我建议您检查您的手机/模拟器与互联网的连接(例如:将提到的 URL 放在手机浏览器窗口中并检查它是否有效)。

于 2012-11-08T19:36:05.330 回答