Skip to content

Commit 0d89bb4

Browse files
committed
Update android example.
Show the full stacktrace when error happens and make the result text view scrollable.
1 parent bcf6cc5 commit 0d89bb4

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

examples/android/helloworld/app/src/main/java/io/grpc/helloworldexample/HelloworldActivity.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import android.os.Bundle;
3737
import android.support.v7.app.ActionBarActivity;
3838
import android.text.TextUtils;
39+
import android.text.method.ScrollingMovementMethod;
3940
import android.view.View;
4041
import android.view.inputmethod.InputMethodManager;
4142
import android.widget.Button;
@@ -48,6 +49,8 @@
4849
import io.grpc.examples.helloworld.HelloReply;
4950
import io.grpc.examples.helloworld.HelloRequest;
5051

52+
import java.io.PrintWriter;
53+
import java.io.StringWriter;
5154
import java.util.concurrent.TimeUnit;
5255

5356
public class HelloworldActivity extends ActionBarActivity {
@@ -66,6 +69,7 @@ protected void onCreate(Bundle savedInstanceState) {
6669
mPortEdit = (EditText) findViewById(R.id.port_edit_text);
6770
mMessageEdit = (EditText) findViewById(R.id.message_edit_text);
6871
mResultText = (TextView) findViewById(R.id.grpc_response_text);
72+
mResultText.setMovementMethod(new ScrollingMovementMethod());
6973
}
7074

7175
public void sendMessage(View view) {
@@ -90,22 +94,22 @@ protected void onPreExecute() {
9094
mResultText.setText("");
9195
}
9296

93-
private String sayHello(ManagedChannel channel) {
94-
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
95-
HelloRequest message = HelloRequest.newBuilder().setName(mMessage).build();
96-
HelloReply reply = stub.sayHello(message);
97-
return reply.getMessage();
98-
}
99-
10097
@Override
10198
protected String doInBackground(Void... nothing) {
10299
try {
103100
mChannel = ManagedChannelBuilder.forAddress(mHost, mPort)
104101
.usePlaintext(true)
105102
.build();
106-
return sayHello(mChannel);
103+
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(mChannel);
104+
HelloRequest message = HelloRequest.newBuilder().setName(mMessage).build();
105+
HelloReply reply = stub.sayHello(message);
106+
return reply.getMessage();
107107
} catch (Exception e) {
108-
return "Failed... : " + e.getMessage();
108+
StringWriter sw = new StringWriter();
109+
PrintWriter pw = new PrintWriter(sw);
110+
e.printStackTrace(pw);
111+
pw.flush();
112+
return "Failed... : " + System.lineSeparator() + sw;
109113
}
110114
}
111115

examples/android/helloworld/app/src/main/res/layout/activity_helloworld.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
android:id="@+id/grpc_response_text"
5050
android:layout_width="match_parent"
5151
android:layout_height="match_parent"
52+
android:scrollbars = "vertical"
5253
android:textSize="16sp" />
5354

54-
</LinearLayout>
55+
</LinearLayout>

0 commit comments

Comments
 (0)