我在 Retrofit 中解析 Json 时遇到问题。我的网址: http: //192.168.0.102 :8000/api/get_services?keyword=someword
Someword 这里是用户在对话窗口中写的单词。
我只需要
- 坐标
- 地址
- 公司名称
在这里,我的坐标为空。我想把结果显示给谷歌地图。
怎么了?我该如何解决问题?
JSON:
LocationActivity.java
private void showInputDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog_search, null);
builder.setView(dialogView);
final EditText inputSearch = (EditText) dialogView.findViewById(R.id.input_service);
inputSearch.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setPositiveButton("Поиск", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String SearchPosition = inputSearch.getText().toString();
if (SearchPosition.matches("")) { //Check if EditText is empty
Toast.makeText(getApplicationContext(), "Space is empty!", Toast.LENGTH_SHORT).show();
return;
} else {
NetworkFactory.getInstance().getApiCall().getPosition(new Words(SearchPosition), new Callback<PositionResponse>() {
@Override
public void success(PositionResponse positionResponse, Response response) {
Log.d("Result: ", Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", ""));
if (positionResponse.getPosition() == null) {
//Toast Message
Toast.makeText(getApplicationContext(), "There is no such result! Try again!", Toast.LENGTH_LONG).show();
} else {
String mLatLng = Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", "");
String[] arrayLatLng = mLatLng.split(","); //separate out lat and lon
LatLng positionLatLon = new LatLng(Double.parseDouble(arrayLatLng[0]), Double.parseDouble(arrayLatLng[1])); //create the LatLng object
mGoogleMap.addMarker(new MarkerOptions().position(positionLatLon)
.title("Result")
.snippet("Result of search: " + SearchPosition)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
CameraPosition cameraPosition = new CameraPosition.Builder().target(positionLatLon).zoom(17).build();
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
@Override
public void failure(RetrofitError error) {
error.toString();
}
});
}
}
});
builder.show();
}
网络工厂.java
public class NetworkFactory {
public static final String API_URL = "http://192.168.0.102:8000";
private static NetworkFactory ourInstance = new NetworkFactory();
private ApiCalls apiCalls;
private NetworkFactory() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.build();
apiCalls = restAdapter.create(ApiCalls.class);
}
public static NetworkFactory getInstance() {
return ourInstance;
}
public ApiCalls getApiCall() {
return apiCalls;
}
}
ApiCalls.java
public interface ApiCalls {
@GET("/api/get_services")
void getPosition(@Query("keyword") Words words, Callback<PositionResponse> responseCallback);
}
PositionResponse.java
public class PositionResponse {
private double[] coords;
private String address;
private String company_name;
public double[] getPosition() {
return coords;
}
public void setPosition(double[] coords) {
this.coords = coords;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCompanyName() {
return company_name;
}
public void setCompanyName(String company_name) {
this.company_name = company_name;
}
}