使用Android Studio 3.0.1,尝试从Gloogle Cloud PubSub模块中提取消息,使用下一代码检索消息:
PullMessage.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.util.Log;
import io.grpc.ManagedChannel;
import io.grpc.okhttp.NegotiationType;
import io.grpc.okhttp.OkHttpChannelBuilder;
import io.grpc.auth.MoreCallCredentials;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.PullRequest;
import com.google.pubsub.v1.PullResponse;
import com.google.pubsub.v1.ReceivedMessage;
import com.google.pubsub.v1.SubscriberGrpc;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import android.widget.ListView;
import android.widget.ArrayAdapter;
public class PullMessage extends AsyncTask<String, Integer, Integer> {
ProgressBar pb = null;
ListView lwListado;
private final String TAG = getClass().getSimpleName();
private Context context;
Integer numMessages;
private List<String> arrayList;
public void setContext(Context ctx){
context=ctx;
}
protected void onPreExecute(){
pb = (ProgressBar) ((Activity)context).findViewById(R.id.progressBar);
pb.setVisibility(View.VISIBLE);
Toast.makeText(context, "Bajando mensajes de la subscripcion: "+MainActivity.SUBSCRIPTION, Toast.LENGTH_LONG).show();
arrayList = new ArrayList<String>();
}
@Override
protected Integer doInBackground(String...params){
try{
Log.d(TAG, "Leyendo archivo de credenciales: "+MainActivity.CREDENTIALS_FILE);
AssetManager am = context.getAssets();
InputStream isCredentials = am.open(MainActivity.CREDENTIALS_FILE);
ManagedChannel channelImpl = OkHttpChannelBuilder.forAddress("pubsub.googleapis.com",443).negotiationType(NegotiationType.TLS).build();
Log.d(TAG, "Canal Terminado?:"+ channelImpl.isTerminated());
GoogleCredentials credential = GoogleCredentials.fromStream(isCredentials);
credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
SubscriberGrpc.SubscriberBlockingStub subscriberBlockingStub = SubscriberGrpc.newBlockingStub(channelImpl).withCallCredentials(MoreCallCredentials.from(credential));
PullRequest pullRequest = PullRequest.newBuilder()
.setSubscription( MainActivity.SUBSCRIPTION )
.setReturnImmediately( true )
.setMaxMessages(10)
.build();
PullResponse pullResponse = subscriberBlockingStub.pull(pullRequest);
numMessages = pullResponse.getReceivedMessagesCount();
Log.d(TAG, "Number of messages received: " + String.valueOf(numMessages));
for (ReceivedMessage message1 : pullResponse.getReceivedMessagesList()) {
PubsubMessage pubsubMessage1 = message1.getMessage();
arrayList.add(pubsubMessage1.toString());
Log.d(TAG, "Message received (ID="+message1.getAckId()+"): " + pubsubMessage1.toString());
}
}catch (IOException e){
Log.d(TAG, "Excepcion: "+e.toString());
}
return 0;
}
//@Override
protected void onProgressUpdate(Integer num){
}
@Override
protected void onPostExecute(Integer num){
pb.setVisibility(View.GONE);
final ArrayAdapter<String>arrayAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, arrayList);
lwListado.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
Toast.makeText(context, "Number of messages received: " + String.valueOf(num) , Toast.LENGTH_LONG).show();
}
}
代码在执行时间内收到错误:
PullResponse pullResponse = subscriberBlockingStub.pull(pullRequest);
完整的错误堆栈是:
FATAL EXCEPTION: AsyncTask #1
Process: com.example.arkmind_2.googlecloud, PID: 2769
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:325)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: io.grpc.StatusRuntimeException: CANCELLED: Failed to read message.
at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:221)
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:202)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:131)
at com.google.pubsub.v1.SubscriberGrpc$SubscriberBlockingStub.pull(SubscriberGrpc.java:897)
at com.example.arkmind_2.googlecloud.PullMessage.doInBackground(PullMessage.java:92)
at com.example.arkmind_2.googlecloud.PullMessage.doInBackground(PullMessage.java:47)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.NoSuchMethodError: No static method checkNotNull(Ljava/lang/Object;)Ljava/lang/Object; in class Lcom/google/protobuf/Internal; or its super classes (declaration of 'com.google.protobuf.Internal' appears in /data/app/com.example.arkmind_2.googlecloud-1/split_lib_dependencies_apk.apk)
at com.google.protobuf.MapField$MutatabilityAwareMap.put(MapField.java:334)
at com.google.pubsub.v1.PubsubMessage.<init>(PubsubMessage.java:66)
at com.google.pubsub.v1.PubsubMessage.<init>(PubsubMessage.java:14)
at com.google.pubsub.v1.PubsubMessage$1.parsePartialFrom(PubsubMessage.java:1197)
at com.google.pubsub.v1.PubsubMessage$1.parsePartialFrom(PubsubMessage.java:1192)
at com.google.protobuf.CodedInputStream.readMessage(CodedInputStream.java:497)
at com.google.pubsub.v1.ReceivedMessage.<init>(ReceivedMessage.java:61)
at com.google.pubsub.v1.ReceivedMessage.<init>(ReceivedMessage.java:13)
at com.google.pubsub.v1.ReceivedMessage$1.parsePartialFrom(ReceivedMessage.java:752)
at com.google.pubsub.v1.ReceivedMessage$1.parsePartialFrom(ReceivedMessage.java:747)
at com.google.protobuf.CodedInputStream.readMessage(CodedInputStream.java:497)
at com.google.pubsub.v1.PullResponse.<init>(PullResponse.java:56)
at com.google.pubsub.v1.PullResponse.<init>(PullResponse.java:13)
at com.google.pubsub.v1.PullResponse$1.parsePartialFrom(PullResponse.java:874)
at com.google.pubsub.v1.PullResponse$1.parsePartialFrom(PullResponse.java:869)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:90)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
at io.grpc.protobuf.lite.ProtoLiteUtils$2.parseFrom(ProtoLiteUtils.java:179)
at io.grpc.protobuf.lite.ProtoLiteUtils$2.parse(ProtoLiteUtils.java:171)
at io.grpc.protobuf.lite.ProtoLiteUtils$2.parse(ProtoLiteUtils.java:88)
at io.grpc.MethodDescriptor.parseResponse(MethodDescriptor.java:266)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1MessagesAvailable.runInContext(ClientCallImpl.java:526)
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
at io.grpc.stub.ClientCalls$ThreadlessExecutor.waitAndDrain(ClientCalls.java:626)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:122)
at com.google.pubsub.v1.SubscriberGrpc$SubscriberBlockingStub.pull(SubscriberGrpc.java:897)
at com.example.arkmind_2.googlecloud.PullMessage.doInBackground(PullMessage.java:92)
at com.example.arkmind_2.googlecloud.PullMessage.doInBackground(PullMessage.java:47)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
D/EGL_emulation: eglMakeCurrent: 0x9aa1e520: ver 2 0 (tinfo 0x9aa23950)
最有趣的部分是这一行:
java.lang.NoSuchMethodError: No static method checkNotNull(Ljava/lang/Object;)Ljava/lang/Object; in class Lcom/google/protobuf/Internal; or its super classes (declaration of 'com.google.protobuf.Internal' appears in /data/app/com.example.arkmind_2.googlecloud-1/split_lib_dependencies_apk.apk)
该程序使用protobuf lite,但似乎缺少某些东西。