Volley get请求没有命中服务器

时间:2017-07-06 21:04:09

标签: java android http android-volley

我在这个问题已经过去了2个小时,基本上我试图从我的服务器获取一些数据并且凌空回调永远不会被执行。

基本上我有一个定义的接口来处理凌空请求,所以对于getRequest我有这样的东西:

public void getDataObjectVolley(final String requestType, String url, final String token){
        try {
            RequestQueue queue = Volley.newRequestQueue(mContext);

            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    if(mResultCallback != null)
                        mResultCallback.notifySuccess(requestType, response);
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {

                @Override
                public Map<String, String> getHeaders(){
                    Map<String, String> headers = new HashMap<>();
                    Log.d("tokenXX",token);
                    headers.put("x-access-token", token);
                    return headers;
                }
            };

            jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            queue.add(jsonObjectRequest);

        }catch(Exception e){

        }
    }

然后在我的活动中我初始化回调并将函数传递给他:

IResult mResultCallbackGET = null;

onCreate我调用loadPhotos

 loadPhotoDetails();

然后方法是这样的:

 private void loadPhotoDetails() {
        final String urlDetails = connectionTxt + "/fotos/" + fotoId;
        loadPhotoInfo();
        mVolleyService = new VolleyService(mResultCallbackGET,this);
        mVolleyService.getDataObjectVolley(GETREQUEST,urlDetails,token);
    }

loadPhotoInfo

void loadPhotoInfo(){
        Log.d("HELLOWORLD","HELLOWOLD");
        mResultCallbackGET = new IResult() {
            @Override
            public void notifySuccess(String requestType, JSONObject response) {
                /*Log.d("HELLOWOLRD","HELLOWOLD1");
                Log.d("resposta123",response.toString());
                String path = null;
                Double lat = null;
                Double lon = null;
                Double alt = null;
                Boolean hasFlower = null;
                Boolean hasLeaf = null;
                Boolean hasThorn = null;
                Boolean hasFruit = null;
                String flowerColor = null;
                String time = null;

                try {
                    path = response.getString("image");
                    lat = response.getDouble("lat");
                    lon = response.getDouble("lon");
                    alt = response.getDouble("altitude");
                    hasFlower = response.getBoolean("hasFlower");
                    hasLeaf = response.getBoolean("hasLeaf");
                    hasThorn = response.getBoolean("hasThorn");
                    hasFruit = response.getBoolean("hasFruit");
                    flowerColor = response.getString("flowerColor");
                    time = response.getString("date");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                photoDetails = new Photo(path,lat,lon,alt,time,hasFlower,hasLeaf,hasThorn,hasFruit,flowerColor);
                Log.d("fruit12",String.valueOf(hasFlower));
                Log.d("fruit12",String.valueOf(hasLeaf));
                Log.d("fruit12",String.valueOf(hasThorn));

                if(hasFruit){
                    hasFruitTxt.setText("Sim");
                }
                else{
                    Log.d("fruit","false");
                    hasFruitTxt.setText("Não");
                }

                if(photoDetails.getHasFlower()){
                    hasFlowerTxt.setText("Sim");
                }
                else{
                    hasFlowerTxt.setText("Não");
                }

                if(photoDetails.getHasThorns()){
                    hasThornsTxt.setText("Sim");
                }
                else{
                    hasThornsTxt.setText("Não");
                }

                if(photoDetails.getHasLeaf()){
                    hasLeafTxt.setText("Sim");
                }
                else{
                    hasLeafTxt.setText("Não");
                }*/
                Log.d("HELLOWOLRD","HELLOWOLD1");
            }

            @Override
            public void notifySuccess(String requestType, JSONArray response) {
                Log.d("HELLOWOLRD","HELLOWOLD2");
            }

            @Override
            public void notifyError(String requestType, VolleyError error) {
                Log.d("HELLOWOLRD","HELLOWOLD3");
            }
        };
    }

你们可以看到我有4个hello worlds日志,它只是进入第一个,它从未到达服务器,对此有任何帮助吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

我的建议是在loadPhotoDetails()方法中处理所有HTTP请求。代码会更短。您可以创建CONSTANTS文件来处理connectionTxt和其他常量。 (参见下面的例子)

// --------------------------------------------- -----------

private void loadPhotoDetails()(String fotoId){

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
            connectionTxt + "/fotos/" + fotoId, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try{
                if (response.has("Ratings")) {
                    JSONArray ratings = response.getJSONArray("Ratings");

                    String source = null;
                    String value = null;
                    if (ratings.length() > 0) {

                        JSONObject mRatings = ratings.getJSONObject(ratings.length() - 1);
                        source = mRatings.getString("Source");
                        value = mRatings.getString("Value");
                        rating.setText(source + " : " + value);

                    }else {
                        rating.setText("Ratings: N/A");
                    }

                    path.setText(response.getString("image"));
                    lat.setText(response.getString("lat"));
                    lon.setText(response.getString("lon"));
                    alt.setText(response.getString("altitude"));
                    hasFlower.setText(response.getBoolean("hasFlower"));
                    hasLeaf.setText(response.getBoolean("hasLeaf"));
                    hasThorn.setText(response.getBoolean("hasThorn"));
                    hasFruit.setText(response.getBoolean("hasFruit"));
                    flowerColor.setText(response.getString("flowerColor"));
                    time.setText(response.getString("date"));
                }

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

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("Error:", error.getMessage());
        }
    });

    queue.add(jsonObjectRequest);

}

// -----------------------------------------