Android排球。从HTTP响应的标头中获取数据

时间:2017-08-20 12:28:14

标签: java android android-volley

我正在向我的REST服务器发出http请求。作为回应,我得到了JSON的身体。但我需要从响应头中获取一个参数,因为我将用户令牌保留在其中。我已经查看了Stack Overflow上很多关于类似问题的帖子,但我似乎没有帮助过我。我想发出一个JSON请求,并从响应中获取正文和标题。我该怎么做?这是我的代码:

请不要将问题标记为重复,因为我没有找到任何可以检索两者的示例:响应标头和响应正文。对于现有问题,用户只能获得标题

   JsonObjectRequest req = new JsonObjectRequest(AppConfig.URL_LOGIN, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    System.out.println(response.toString());
                    hideDialog();

                    try {
                        JSONObject jObj = response;

                        String uid = jObj.getString("_id");
                        String name = jObj.getString("fullName");
                        String email = jObj.getString("email");

                        // Inserting row in users table
                        db.addUser(name, email, uid);

                        Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();

                        Intent intent = new Intent(
                                LoginActivity.this,
                                MainActivity.class);
                        startActivity(intent);
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            NetworkResponse networkResponse = error.networkResponse;
            String toastError = "Response code: " + networkResponse.statusCode;
            Toast.makeText(getApplicationContext(),
                    toastError, Toast.LENGTH_LONG).show();

            hideDialog();
        }
    });

1 个答案:

答案 0 :(得分:1)

您无法使用JsonObjectRequest执行此操作。您应该扩展Request类并实现parseNetworkResponse()方法,该方法提供对低级NetworkResponse对象的访问。下行当然是你必须重新实现JSON解析,但这不是什么大问题。

类似的东西:

public class HeaderAwareJsonRequest extends Request<Pair<JSONObject,Map>> {
  protected Response.Listener<Pair<JSONObject,Map>> listener;

  public HeaderAwareJsonRequest( int method, String url, Response.Listener<Pair<JSONObject,Map>> listener, Response.ErrorListener errorListener ) {
    super( method, url, errorListener );
    this.listener = listener;
  }

  @Override
  protected Response<Pair<JSONObject,Map>> parseNetworkResponse( NetworkResponse response ) {
    try{
      String jsonString = new String( response.data, HttpHeaderParser.parseCharset( response.headers ) );
      // pair contains the json body and headers 
      Pair pair = new Pair( new JSONObject( jsonString ), response.headers );
      return Response.success( pair, HttpHeaderParser.parseCacheHeaders( response ) );
    }catch( Exception e ){
      return Response.error( new ParseError( e ) );
    }
  }    
}

然后在调用代码中:

HeaderAwareJsonRequest req = new HeaderAwareJsonRequest( 
   Request.Method.GET, 
   AppConfig.URL_LOGIN, 
   new Response.Listener<Pair<JSONObject,Map>>() {
     @Override
     public void onResponse(Pair<JSONObject,Map> response) {
       JSONObject jObj = response.first;
       Map headers = response.second;
       String someHeader = headers.get( "aaaaa" );
       ...
     }
   }, 
   new Response.ErrorListener() {...} 
);