2016-04-05 29 views
6

我正在使用Volley,我想向服務器發出請求,該服務器在「可分層」(我可以在Web瀏覽器中看到它)返回一個JSON。我的問題是,服務器還返回我的頭信息,我需要在我的應用程序,但我無法從請求中獲取標題。從排隊的響應中獲取頭文件

我尋覓了很長時間,但我還沒有發現任何有用的(Onlye將數據添加到請求頭,而不是從header's響應獲取數據)

任何人都知道如何實現?

回答

12

一個實例在請求中獲取需要重寫parseNetworkResponse的頭文件。

例如JsonObjectRequest:

public class MetaRequest extends JsonObjectRequest { 

    public MetaRequest(int method, String url, JSONObject jsonRequest, Response.Listener 
      <JSONObject> listener, Response.ErrorListener errorListener) { 
     super(method, url, jsonRequest, listener, errorListener); 
    } 

    public MetaRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject> 
      listener, Response.ErrorListener errorListener) { 
     super(url, jsonRequest, listener, errorListener); 
    } 

    @Override 
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { 
     try { 
      String jsonString = new String(response.data, 
        HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); 
      JSONObject jsonResponse = new JSONObject(jsonString); 
      jsonResponse.put("headers", new JSONObject(response.headers)); 
      return Response.success(jsonResponse, 
        HttpHeaderParser.parseCacheHeaders(response)); 
     } catch (UnsupportedEncodingException e) { 
      return Response.error(new ParseError(e)); 
     } catch (JSONException je) { 
      return Response.error(new ParseError(je)); 
     } 
    } 
} 
+0

謝謝!有用!!我正在做parseNetworkResponse中的標題,而不是將標題添加到返回的JSONObject。 – Chopi

1

如果請求返回JSON你應該能夠正常讀取它,如果不是這個問題是從JSON不是從Android應用

這裏是請求處理

RequestQueue queue = Volley.newRequestQueue(MainActivity.this); 
    JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, "http://yourlink+parameters", new Response.Listener<JSONArray>() { 
     @Override 
     public void onResponse(JSONArray response) { 
      List<YourObject> reservations = null; 
      try { 

       YourObject= new ArrayList<YourObject>(); 
       for (int i = 0; i < response.length(); i++) { 
        JSONObject obj = response.getJSONObject(i); 
        YourObjectr = new YourObject(obj.getInt("id"), obj.getInt("user_id"),format.parse(obj.getString("date")), obj.getString("address"), obj.getString("description")); 
         } 

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

       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError volleyError) { 
      if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) { 
       VolleyError error = new VolleyError(new String(volleyError.networkResponse.data)); 
       volleyError = error; 
       Log.i("error", "error :" + volleyError.getMessage()); 
       try { 
        JSONObject obj = new JSONObject(volleyError.getMessage()); 
        if (obj.getString("error").equals("token_expired")) { 
         Intent i = new Intent(MainActivity.this, LoginActivity.class); 
         startActivity(i); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } else { 
       new AlertDialog.Builder(MainActivity.this) 
         .setTitle("connection problem") 
         .setMessage("check your connection") 
         .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
          } 
         }) 
         .setIcon(android.R.drawable.ic_dialog_alert) 
         .show(); 
      } 
     } 
    }); 

    queue.add(getRequest); 
+2

使用JsonArrayRequest,您無法獲取http請求的標頭信息(例如cookie)。 – Chopi

0

這與JSONArray數據和標題的工作的例子。

首先創建自己的自定義請求類型實現:

public class JsonRequest extends JsonObjectRequest { 

    public JsonRequest(int method, String url, JSONObject jsonRequest, Response.Listener 
      <JSONObject> listener, Response.ErrorListener errorListener) { 
     super(method, url, jsonRequest, listener, errorListener); 
    } 

    @Override 
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { 
     try { 
      String jsonString = new String(response.data, 
        HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET)); 

      JSONObject jsonResponse = new JSONObject(); 
      jsonResponse.put("data", new JSONArray(jsonString)); 
      jsonResponse.put("headers", new JSONObject(response.headers)); 

      return Response.success(jsonResponse, 
        HttpHeaderParser.parseCacheHeaders(response)); 
     } catch (UnsupportedEncodingException e) { 
      return Response.error(new ParseError(e)); 
     } catch (JSONException je) { 
      return Response.error(new ParseError(je)); 
     } 
    } 
} 

,並在您請求代碼:

JsonRequest request = new JsonRequest 
     (Request.Method.POST, URL_API, payload, new Response.Listener<JSONObject>() { 

      @Override 
      public void onResponse(JSONObject response) { 
       try { 
        JSONArray data = response.getJSONArray("data"); 
        JSONObject headers = response.getJSONObject("headers"); 
       } catch (JSONException e) { 
        Log.e(LOG_TAG, Log.getStackTraceString(e)); 
       } 
      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e(LOG_TAG, Log.getStackTraceString(error)); 
      } 
     }); 

查看關於貫徹凌空文檔Implementing a Custom Request在自己的自定義要求的更多信息。