从谷歌地图搜索中读取文件json的问题

时间:2011-09-15 16:05:28

标签: android google-maps

请告诉我下面这段代码的错误。我尝试了很多次,但我仍然收到错误:“应用程序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;        
}

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

     }

}

谢谢!

1 个答案:

答案 0 :(得分:0)

我的,你有很多空的。第一个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;

确保您在Manifest中拥有使用许可INTERNET。

Afler所有的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

干杯。