2016-03-11 66 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

相關問題