排球 - 带标题的POST请求

时间:2017-04-05 13:17:43

标签: android http-headers xmlhttprequest android-volley

我尝试使用带标题的volley rquest。我的代码如下:

        final String contentType = "text/xml;charset=UTF-8";


        Request stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.i("@@response ", response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("@@Error ", error);
                }
            }) {

            @Override
            public String getBodyContentType()
            {
                return contentType;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();

                    headers.put("Accept-Encoding", "gzip,deflate");
                    headers.put("Connection", "Keep-Alive");
                    headers.put("User-Agent", "Apache-HttpClient/4.1.1 (java 1.5)");
                return headers;
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return requestBody.getBytes(getParamsEncoding());
                } catch (Exception e) {
                    Log.i("@@ERROR ", " " + e);
                    return null;
                }
            }
        };

我的回答类似于

  

I�%&/m�{J�J��t�� $ @ HIG#)* EVE F的@흼{{ ;?!???N'\fdlJɞ〜|“ezMQ- HWQ /լX ^ |Ѻ=&GT ; 8zT2 / U + GT; YۮݽL“K&GT; W] R7ח〜ķ

编码 - 解码有一些问题。我怎么解决这个问题?如果我评论headers.put("Accept-Encoding", "gzip,deflate"),我可以得到确切的回复。

1 个答案:

答案 0 :(得分:0)

我找到了这个答案,

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

public class GZipRequest extends StringRequest {

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

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

    // parse the gzip response using a GZIPInputStream
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String output = ""; // note: better to use StringBuilder
        try {
            final GZIPInputStream gStream = new GZIPInputStream(new ByteArrayInputStream(response.data));
            final InputStreamReader reader = new InputStreamReader(gStream);
            final BufferedReader in = new BufferedReader(reader);
            String read;
            while ((read = in.readLine()) != null) {
                output += read;
            }
            reader.close();
            in.close();
            gStream.close();
        } catch (IOException e) {
            return Response.error(new ParseError());
        }
        return Response.success(output, HttpHeaderParser.parseCacheHeaders(response));
    }
}

来自此链接https://gist.github.com/premnirmal/8526542