如何申请使用protobuf的凌空? [英] how to request protobuf using volley?

查看:147
本文介绍了如何申请使用protobuf的凌空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人有一个例子,你读的protobuf,使用凌空?我已阅读,你可以实现ProtobufRequest类,但我不能为它找到文档。谢谢你的帮助。

anyone has an example, where you read a protobuf, using volley?. I have read that you can implement ProtobufRequest class, but I can not find documentation for it. Thank you for your help.

推荐答案

我都做到了,工作是好的,希望它是helpful.first创建ProtobufRequest类象下面这样扩展请求,请求是基本要求类凌空,那么,你可以创建自定义请求延长ProtobufRequest那些定制doParse()。只是作为一个参考。

I have done it,the work is ok,hope it is helpful.first create the ProtobufRequest class like below that extends Request,Request is the basic request class in Volley,then,you can create the custom request extends ProtobufRequest that do custom doParse().just as a reference.

是的RequestData消息SomeProto,

RequestData is SomeProto message,

private static final int SOCKET_TIMEOUT = 30000;

/** Content type for request. */
private static final String PROTOCOL_CONTENT_TYPE = "application/x-protobuf";

private static final Object sDecodeLock = new Object();

private RequestData mRequestData;
private BaseCallback mCallback;

public ProtobufRequest(RequestData data, BaseCallback callback) {
    super(Method.POST, Constants.SERVER_URL, callback);
    // TODO Auto-generated constructor stub
    mRequestData = data;
    mCallback = callback;
    this.setRetryPolicy(getRetryPolicy());
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Charset", "UTF-8");
    headers.put("Content-Type", "application/x-protobuf");
    headers.put("Accept", "application/x-protobuf");

    return headers;
}

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

@Override
public RetryPolicy getRetryPolicy() {
    RetryPolicy retryPolicy = new DefaultRetryPolicy(SOCKET_TIMEOUT,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    return retryPolicy;
}

    /**
    ** write the protobuf data to http request,it is very important
    */
@Override
public byte[] getBody() throws AuthFailureError {
    if (mRequestData == null) {
        return super.getBody();
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {

            baos.write(mRequestData.toByteArray());
        } catch (IOException e) {
            return super.getBody();
        }

        return baos.toByteArray();
    }
}

@Override
protected void deliverResponse(Object arg0) {
    mCallback.onResponse(arg0);
}

    /**
    ** parse the response result
    */
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    synchronized (sDecodeLock) {
        try {
            return doParse(response, HttpHeaderParser.parseCacheHeaders(response));
        } catch (OutOfMemoryError e) {
            return Response.error(new ParseError(e));
        }
    }
}

abstract protected Response<T> doParse(NetworkResponse response, Entry entry)


public interface BaseCallback extends Listener<Object>, ErrorListener {

}

这篇关于如何申请使用protobuf的凌空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆