2016-03-11 70 views
0

我想在我的API查找最近的酒吧和restoraunts谷歌地图,并把它放在listView和地图上,我做了改造,有点我有一个错误,,但我不明白我该怎么做,你能帮我吗?最近的酒吧和餐厅从api谷歌地图改造

UPD所有代码:

UPD添加适配器代码

公共类改造{

private static final String ENDPOINT = "https://maps.googleapis.com/maps/api/place"; 
private static ApiInterface apiInterface; 

interface ApiInterface { 
    @GET("/nearbysearch/json?location=49.9944422,36.2368201&radius=500&types=food&key=AIzaSyDQZoJKznf0uRkvJFqLYbZ7-1GeGudjmv0") 
    void getBars(Callback<PlaceResponse> callback); 


} 

static { 
    init(); 
} 

private static void init() { 
    RestAdapter restAdapter = new RestAdapter.Builder() 
      .setEndpoint(ENDPOINT) 
      .setLogLevel(RestAdapter.LogLevel.FULL) 
      .build(); 
    apiInterface = restAdapter.create(ApiInterface.class); 
} 

public static void getBars(Callback<PlaceResponse> callback) { 
    apiInterface.getBars(callback); 
} 

}

类结果:

public class Result { 

public Geometry geometry; 
public String icon; 
public String id; 
public String name; 
public OpeningHours openingHours; 
public List<Photo> photos = new ArrayList<Photo>(); 
public String placeId; 
public String scope; 
public List<AltId> altIds = new ArrayList<AltId>(); 
public String reference; 
public List<String> types = new ArrayList<String>(); 
public String vicinity; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getVicinity() { 
    return vicinity; 
} 

public void setVicinity(String vicinity) { 
    this.vicinity = vicinity; 
} 

}

类地回应:

public class PlaceResponse { 

public List<Object> htmlAttributions = new ArrayList<Object>(); 
public List<Result> results = new ArrayList<Result>(); 
public String status; 

}

呼叫和转接器设置:

Retrofit.getBars(new Callback<PlaceResponse>() { 
     @Override 
     public void success(PlaceResponse placeResponse, Response response) { 
      listView.setAdapter(new MyAdapter(MainActivity.this, placeResponse // There is error)); 
     } 

     @Override 
     public void failure(RetrofitError error) { 
      Toast.makeText(MainActivity.this, "Something Wrong", Toast.LENGTH_SHORT).show(); 

     } 
    }); 

错误:

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 

适配器代码:

class MyAdapter extends ArrayAdapter<Result> { 

    public MyAdapter(Context context, List<Result> objects) { 
     super(context, R.layout.list_item, objects); 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     View rowView = convertView; 
     if (rowView == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      rowView = inflater.inflate(R.layout.list_item, parent, false); 
      holder = new ViewHolder(); 
      holder.textNameOfBar = (TextView) rowView.findViewById(R.id.name_id); 
      holder.textVicinity = (TextView) rowView.findViewById(R.id.vicinity_id); 

      rowView.setTag(holder); 
     } else { 
      holder = (ViewHolder) rowView.getTag(); 
     } 

     Result result = getItem(position); 
     holder.textNameOfBar.setText(result.getName()); 
     holder.textVicinity.setText(result.getVicinity()); 


     return rowView; 
    } 

    class ViewHolder { 

     public TextView textNameOfBar; 
     public TextView textVicinity; 

    } 


} 
+0

有什么错误? –

+0

我的不好,现在addad – Azarnoy

+0

所以基本上这个错误是由于你的API密钥在开发者控制台中不包含相应的ip地址造成的。添加您的IP地址对您的API密钥,几分钟后尝试!它将工作完美 –

回答

0

因为你预期的结果(List<Bars>)也不是什么服务器返回(即你的除外JSON数组,但服务器返回一个JSON对象),您收到的错误。

place search documentation显示响应具有封装结果的给定格式。在您的API接口

public class PlaceResponse { 

    public List<Object> htmlAttributions = new ArrayList<Object>(); 
    public List<Result> results = new ArrayList<Result>(); 
    public String status; 
} 

... 

public class Result { 

    public Geometry geometry; 
    public String icon; 
    public String id; 
    public String name; 
    public OpeningHours openingHours; 
    public List<Photo> photos = new ArrayList<Photo>(); 
    public String placeId; 
    public String scope; 
    public List<AltId> altIds = new ArrayList<AltId>(); 
    public String reference; 
    public List<String> types = new ArrayList<String>(); 
    public String vicinity; 

} 

void getBars(Callback<PlaceResponse> callback);:在这种情况下,模型类应该这样。

您可以使用jsonschema2pojo从json模式生成模型类。

此外,请注意食物类型已弃用(请参阅Place Types)。

然后在改造回调,你可以做访问结果:

@Override 
    public void success(PlaceResponse placeResponse, Response response) { 
     listView.setAdapter(new MyAdapter(MainActivity.this, placeResponse.getResults())); 
    } 
+0

,我应该在适配器中执行哪些操作?在更新中添加了一个错误,还添加了新的类 – Azarnoy

+0

@Azarnoy只需使用'PlaceResponse'内的结果,那就是您想要使用的对象。我用正确的代码更新了我的答案。 – Bhullnatik

相关问题