请告诉我下面的代码在哪里错误。我尝试了很多次,但仍然收到错误:“应用程序demojson_google意外停止”,并且没有显示所有内容。这是我的代码:

班级成绩

    package com.javacode.android.json.model;
    import com.google.gson.annotations.SerializedName;
    public class Result {
    @SerializedName("GsearchResultClass")
    public String GsearchResultClass;

    @SerializedName("viewportmode")
    public String viewportmode;

    @SerializedName("listingType")
    public String listingType;

    @SerializedName("lat")
    public String lat;

    @SerializedName("lng")
    public String lng;

    @SerializedName("title")
    public String title;
}


类SearchResponse

package com.javacode.android.json.model;
import java.util.List;

public class SearchResponse {
    public List<Result> results;
}


类GoogleSearchActivity

package com.javacode.android.json;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;
import com.javacode.android.json.model.R;
import com.javacode.android.json.model.Result;
import com.javacode.android.json.model.SearchResponse;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class GoogleSearchActivity extends Activity {
    /** Called when the activity is first created. */
    String url = "ajax.googleapis.com/ajax/services/search/local?v=1.0&q=train&sll=50.00001,-5.00001";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        InputStream source = retrieveStream(url);

        Gson gson = new Gson();

        Reader reader = new InputStreamReader(source);

        SearchResponse response = gson.fromJson(reader, SearchResponse.class);

        List<Result> results = response.results;

        for (Result result : results) {
            Toast.makeText(this, result.title, Toast.LENGTH_SHORT).show();
        }

    }

    private InputStream retrieveStream(String url) {

        DefaultHttpClient client = new DefaultHttpClient();

        HttpGet getRequest = new HttpGet(url);

        try {

           HttpResponse getResponse = client.execute(getRequest);
           final int statusCode = getResponse.getStatusLine().getStatusCode();

           if (statusCode != HttpStatus.SC_OK) {
              Log.w(getClass().getSimpleName(),
                  "Error " + statusCode + " for URL " + url);
              return null;
           }

           HttpEntity getResponseEntity = getResponse.getEntity();
           return getResponseEntity.getContent();

        }
        catch (IOException e) {
           getRequest.abort();
           Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
        }

        return null;

     }

}


谢谢!

最佳答案

我的我,你有很多空值。第一个空值来自retrieveStream。尝试这个:

    InputStream is = null;

    DefaultHttpClient client = new DefaultHttpClient();

    HttpGet getRequest = new HttpGet(url);

    try {

       HttpResponse getResponse = client.execute(getRequest);
       final int statusCode = getResponse.getStatusLine().getStatusCode();

       if (statusCode != HttpStatus.SC_OK) {
          Log.w(getClass().getSimpleName(),
              "Error " + statusCode + " for URL " + url);

       }
       else
       {
           HttpEntity getResponseEntity = getResponse.getEntity();
           is = getResponseEntity.getContent();
       }

    }
    catch (IOException e) {
       getRequest.abort();
       Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
    }

    return is;


确保清单中有使用许可的INTERNET。

另外,您所有的SearchResponse也为null。
我没有用过gson。为什么不考虑使用好旧的:

        HttpGet httpget = new HttpGet(url);

    HttpResponse response;

    response = httpclient.execute(httpget);

    HttpEntity entity = response.getEntity();

    if (entity != null) {

        InputStream instream = entity.getContent();
        gpResult = new GPResult();

        JSONObject json=new JSONObject(
                convertStreamToString(instream
                ));
        //the rest of JSON object processing


干杯。

09-27 11:40