由于某种原因,这个JSON请求没有通过,我检查我有互联网连接,一切都很好。它曾经完美地工作,我不确定发生了什么,我确实触摸了代码,因为它开始工作。当我没有连接时,它会抛出错误。所有日志都不会发布。我真的不确定发生了什么。谁能帮帮我吗。它出于某种原因会跳过整个请求。谢谢。
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("https://maps.google.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&key=AIzaSyDoj_f_2-gGLy5ClzVQwmtscMy9dW2KoNI",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
String addre = null;
Address addr1 = null;
String address1 = "", address2 = "", city = "", state = "", country = "", county = "", pin = "";
try {
addre = ((JSONArray) jsonObject.get("results")).getJSONObject(0).getString("formatted_address");
if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
// JSONArray Results = jsonObject.getJSONArray("results");
// JSONObject zero = Results.getJSONObject(0);
Log.d("formated address ", "`" + addre);
String[] address = addre.split(",");
for (int i = 0; i < address.length; i++) {
Log.d("address " + i, "`" + address[i]);
}
city = address[1];
state = address[2].split(" ")[1];
country = address[3];
Log.d("addr_label", "`" + addre);
Log.d("city", "`" + city);
Log.d("state", "`" + state);
Log.d("country", "`" + country);
}
if (country.trim().equals("USA")) {
if (city == null && state == null) {
locationView.setText("No Location Found");
} else if (state == null) {
locationView.setText(city);
} else if (city == null) {
locationView.setText(state);
}
locationView.setText(city + ", \n" + state);
} else {
if (country == null && state == null) {
locationView.setText("No Location Found");
} else if (state == null) {
locationView.setText(country);
} else if (country == null) {
locationView.setText(state);
}
locationView.setText(state + ", \n" + country);
}
if (locationView.getText().toString().trim().isEmpty()) {
locationView.setText("No Location Found");
}
Log.d("Location View", "`" + locationView.getText());
} catch (JSONException e) {
e.printStackTrace();
Log.d("Location", "catch");
locationView.setText("No Location Found");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
requestQueue.add(jsonObjectRequest);
再次感谢。