排球请求无法启动请求

时间:2016-02-25 15:42:26

标签: java android json android-volley

当我尝试发出请求时遇到此问题我没有收到任何错误,但它没有提出请求。为了测试它我添加了一个pritln System.out.println("DB daily Test 2");但它没有显示。也许有人会发现我做错了什么?

JsonObjectRequest jorDaily = new JsonObjectRequest(Request.Method.GET, "http://api.openweathermap.org/data/2.5/forecast/daily?id=2965140&appid=eacc664602550623c7fe93a2732ad127" ,null,

            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {




                    ArrayList<WeatherCondition>  weatherList = new ArrayList<WeatherCondition>();
                    System.out.println("DB daily Test 2");
                    // Trying to extract the imnformation from the JSON response
                    try {
                        JSONObject cityObj = response.getJSONObject("city");
                        JSONObject coordObj = response.getJSONObject("coord");
                        Coordinates coord = new Coordinates(coordObj.getString("lat"),coordObj.getString("lon"));
                        JSONArray list = response.getJSONArray("list");

                        com.example.tadas.betterweather4.City city = new com.example.tadas.betterweather4.City(cityObj.getString("id"),cityObj.getString("name"),cityObj.getString("country"),coord);
                        System.out.println("DB daily Test 3");
                        for (int i = 0; i < list.length(); i++) {

                            String date;
                            String time;

                            JSONObject childJSONObject = list.getJSONObject(i);
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTimeZone(TimeZone.getDefault());
                            calendar.setTimeInMillis(childJSONObject.getInt("dt") * 1000);

                            date = "" + calendar.get(Calendar.YEAR) + "/"+ calendar.get(Calendar.MONTH)+"/"+calendar.get(Calendar.DAY_OF_MONTH);
                            time = calendar.get(Calendar.HOUR_OF_DAY) + ":00";

                            JSONObject tempObj = childJSONObject.getJSONObject("temp");
                            JSONObject weatherObj = childJSONObject.getJSONObject("weather");

                            Wind wind = new Wind(childJSONObject.getString("deg"), childJSONObject.getString("speed"));

                            WeatherCondition w = new WeatherCondition(
                                    weatherObj.getString("icon"),
                                    childJSONObject.getString("humidity")+"%",
                                    "",
                                    weatherObj.getString("description"),
                                    "",
                                    childJSONObject.getString("clouds"),
                                    tempObj.getString("temp_main"),
                                    tempObj.getString("temp_max"),
                                    date,
                                    time,
                                    city,
                                    wind
                            );
                            w.setCurrent(tempObj.getString("temp"));

                            weatherList.add(w);
                            System.out.println("DB daily Test 4");
                        }

                        DBHelper db = DBHelper.getInstance(context);


                        for (WeatherCondition w: weatherList)
                        {
                            db.insertDaily(w);
                        }



                    } catch (JSONException e) {
                        e.printStackTrace();
                    }





                }
            },


        new Response.ErrorListener() {
            @Override
             public void onErrorResponse(VolleyError error) {
                System.out.println("DB daily Test 00000000000000");
             }

        }

 );

1 个答案:

答案 0 :(得分:0)

您只是创建请求而不将其添加到队列中。 Volley.newRequestQueue(context).add(jorDaily);

请记住使用RequestQueue的静态实例并将其重用于所有请求的良好做法。

相关问题