有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Json请求不起作用不确定原因

由于某种原因,这个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);

再次感谢


共 (0) 个答案