简体   繁体   English

使用Volley的库从HTTP响应解析标头(Android)

[英]Using Volley's library to Parse Headers from HTTP Response (Android)

I'm hoping to use Volley's library to include headers from a response in Android. 我希望使用Volley的库来包含Android响应中的标头。 I know there are other tools like OkHttp, Retrofit, Moshi, Picasso that have more documentation for this (one of the cons of Volley I think -- little documentation). 我知道还有其他工具,例如OkHttp,Retrofit,Moshi,Picasso,对此有更多文档(我认为Volley的缺点之一-很少文档)。 Yet I've already vested a lot of time into it. 但是我已经花了很多时间了。

I just want to translate a curl --include -X POST -d ... request into android. 我只想将curl --include -X POST -d ...请求转换为android。

So far, I have the POST working, but not the --include (helpful tutorial at https://www.itsalif.info/content/android-volley-tutorial-http-get-post-put ): 到目前为止,我可以进行POST了,但是--include却没有。(位于https://www.itsalif.info/content/android-volley-tutorial-http-get-post-put的有用教程):

public class HTTPService {

public static void makeRequest(Context context) {
    RequestQueue queue = Volley.newRequestQueue(context);

    Response.Listener responseListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if (response!=null){
                Log.d("Response", response);
                Log.d("Response", Integer.toString(response.length()));
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // error
            String response = "Error";
            Log.d("Error.Response", error.toString());
        }
    };

    String url = "http://httpbin.org/post";


    StringRequest postRequest = new StringRequest(Request.Method.POST, url, responseListener, errorListener) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", "John");
            params.put("domain", "https://www.facebook.com");
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return super.getHeaders();
        }
        };
    queue.add(postRequest);
}
}

I have shown the getHeaders function to illustrate that this is NOT what I want. 我已经展示了getHeaders函数来说明这不是我想要的。 That is to put headers ON your data. 那就是在数据上放置标题。 I want to INCLUDE HTTP headers in my response object. 我想在响应对象中包含HTTP标头。

Two links I have found are supposed to describe how to do this: 我发现的两个链接应该描述如何执行此操作:

1) https://developer.android.com/training/volley/request-custom.html 1) https://developer.android.com/training/volley/request-custom.html

2) https://android.googlesource.com/platform/frameworks/volley/+/android-4.3_r0.9/src/com/android/volley/toolbox/JsonObjectRequest.java 2) https://android.googlesource.com/platform/frameworks/volley/+/android-4.3_r0.9/src/com/android/volley/toolbox/JsonObjectRequest.java

But I get very confused at this point and... could anyone provide a working solution? 但是我现在很困惑,...谁能提供可行的解决方案? Or tell me how they address the curl -i/--include command? 或者告诉我他们如何解决curl -i /-include命令?

Searching on google, I only see ways to access the header information (like --include) is from within the Response parseNetworkResponse(NetworkResponse response) function from Volley. 在google上搜索时,我仅看到从Volley的Response parseNetworkResponse(NetworkResponse response)函数中访问标头信息(例如--include)的方法。 The following is from user thedude's answer at Android Volley read and store HTTP Header . 以下是Android Volley上用户thedude的答案,其中读取并存储了HTTP Header

@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);

        // set token after receiving from login response
        TokenHandler.setToken(response.headers.get("x-auth"));

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

Because this is a static context, the only way I've found to store the information is as a public variable of another class. 因为这是静态上下文,所以我发现存储信息的唯一方法是作为另一个类的公共变量。 The answer fromm thedude uses a TokenHandler.java class: fromd thedude的答案使用TokenHandler.java类:

public final class TokenHandler {
private TokenHandler() {}

private static String token = "";

public static void setToken(String newToken) {
    if (newToken != null)
        token = newToken;
}

public static String getToken() {
    return token;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM