美文网首页
关于使用GRPC遇到的BUG

关于使用GRPC遇到的BUG

作者: koinzhang | 来源:发表于2019-01-20 23:59 被阅读84次
  1. GRPC获取服务器数据是耗时操作,不能写在UI主线程中,可以写在子线程或使用AsyncTask实现获取数据,但实测,仅仅是获取少量数据,可以写在UI主线程中(虽然不推荐这么做)。目前博主在AsyncTask中的doInBackground()实现获取服务器数据(数据量比较少),再在onPostExecute()中实现对UI的数据加载,会有很长时间的延迟,目前这个问题待解决//TODO。
    下面给出博主的AsyncTask的代码
    class myAsyncTask extends AsyncTask<Void,Void,List<UpdateNews>> {

        @Override
        protected List<UpdateNews> doInBackground(Void... voids) {
            List<UpdateNews> updateNewsList = new ArrayList<UpdateNews>();
            UpdateNews[] messageGot=new UpdateNews[limit];
            ManagedChannel mChannel;
            mChannel = ManagedChannelBuilder.forAddress(HOST, PORT)
                    .usePlaintext()
                    .build();

            DataServiceGrpc.DataServiceBlockingStub stub = DataServiceGrpc.newBlockingStub(mChannel);
            Dataservice.MsgRequest message = Dataservice.MsgRequest.newBuilder()
                    .setStart(start)
                    .setLimit(limit)
                    .setTitle(title_)
                    .setContent(content_)
                    .build();
            Dataservice.MsgResponse response = stub.getNews(message);
            for (int i = 0; i < limit; i++) {
                messageGot[i].setTime(response.getRep(i).getPublishDate());
                messageGot[i].setAbstrnews(response.getRep(i).getTitle());
                updateNewsList.add(messageGot[i]);
            }
            try {
                //关闭channal
                mChannel.shutdown().awaitTermination(1, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return updateNewsList;
        }

        @Override
        protected void onPostExecute(List<UpdateNews> list){
            uNewsList=list;
        }
    }

AsyncTask<Params,Progress,Result>是一个抽象类,通常用于被继承.继承AsyncTask需要指定如下三个泛型参数:
Params:启动任务时输入的参数类型.
Progress:后台任务执行中返回进度值的类型.
Result:后台任务执行完成后返回结果的类型(doInBackground的返回值类型).
.构建AsyncTask子类的回调方法
AsyncTask主要有如下几个方法:
doInBackground:必须重写,异步执行后台线程要完成的任务,耗时操作将在此方法中完成.
onPreExecute:执行后台耗时操作前被调用,通常用于进行初始化操作.
onPostExecute:当doInBackground方法完成后,系统将自动调用此方法,并将doInBackground方法返回的值传入此方法.通过此方法进行UI的更新.
onProgressUpdate:当在doInBackground方法中调用publishProgress方法更新任务执行进度后,将调用此方法.通过此方法我们可以知晓任务的完成进度.

  1. 博主作为一个安卓小白,对于安卓的网络请求不是很熟悉,对于GRPC,遇到很多坑,目前自己也没法解决,先在这里挖坑,日后再填。
      在请求一个自定义类型的数据时,遇到了网络请求错误,错误提示Caused by: io.grpc.StatusRuntimeException: INTERNAL: grpc: error while marshaling: proto: Marshal called with nil,目前网上关于GRPC的资料比较少,具体解决方法目前没找到//TODO。
    完整错误提示:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.example.zyk97.infotest, PID: 27554
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:353)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: io.grpc.StatusRuntimeException: INTERNAL: grpc: error while marshaling: proto: Marshal called with nil
        at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:233)
        at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:214)
        at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:139)
        at com.dib.pann.DataServiceGrpc$DataServiceBlockingStub.getNews(DataServiceGrpc.java:347)
        at com.example.zyk97.infotest.Fragment.Fragment1$myAsyncTask.doInBackground(Fragment1.java:111)
        at com.example.zyk97.infotest.Fragment.Fragment1$myAsyncTask.doInBackground(Fragment1.java:93)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
        at java.lang.Thread.run(Thread.java:764) 

相关文章

  • 关于使用GRPC遇到的BUG

    GRPC获取服务器数据是耗时操作,不能写在UI主线程中,可以写在子线程或使用AsyncTask实现获取数据,但实测...

  • 2018-02-17 Python BUG 之 TypeErro

    初学Python 遇到了一个BUG,关于对象实例化使用的问题。 TypeError: xxx missing 1 ...

  • grpc初探

    1 grpc的定义 grpc good rpc grpc使用protobuf文件声明服务,服务端和客户端都通使用...

  • google grpc 快速入门

    为什么去使用gRPC 使用gRPC,我们可以在.proto文件中定义我们的服务,并以任何gRPC支持的语言实现客户...

  • jedisPool使用遇到的bug

    这个是今天发现一个bug:在测试redis并发读写的时候(jedis作为客户端,并使用了连接池),总是报用完jed...

  • 使用Glide遇到的Bug

    最近在使用Glide的过程中遇到了一个Bug:我在使用填充图片setPlaceHolder(int resID)的...

  • 使用vue遇到的bug

    Vue数据更新UI不刷新显示 Vue.set( target, key, value ) vue比较常见的坑就是数...

  • Hyperledger-Fabric-SDK-设计

    概要 区块链网络使用 gRPC 协议 Protocol Buffers(格式的 API) 使用的协议 gRPC P...

  • python grpc注意事项

    记录几个在使用grpc时遇到的问题: 1. 在.proto文件使用enum时,遇到0无法字符串序列化的问题,表现为...

  • grpc 超时和重连

    最近项目要使用grpc,但是关于grpc的超时和重连这一块很多文章都是说的不够详细,无奈只能自己看代码.顺手记录一...

网友评论

      本文标题:关于使用GRPC遇到的BUG

      本文链接:https://www.haomeiwen.com/subject/kkfqjqtx.html