Skip to content

Commit fbb06ce

Browse files
authored
Merge pull request saturnism#9 from shuza/master
grpc simple android client example added
2 parents d8852f4 + 0fabff5 commit fbb06ce

33 files changed

Lines changed: 916 additions & 0 deletions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/libraries
5+
/.idea/modules.xml
6+
/.idea/workspace.xml
7+
.DS_Store
8+
/build
9+
/captures
10+
.externalNativeBuild
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'com.google.protobuf'
3+
4+
android {
5+
compileSdkVersion 28
6+
defaultConfig {
7+
applicationId "com.example.grpc.client"
8+
minSdkVersion 16
9+
targetSdkVersion 28
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
compileOptions {
21+
targetCompatibility 1.8
22+
sourceCompatibility 1.8
23+
}
24+
}
25+
26+
protobuf {
27+
protoc {
28+
artifact = 'com.google.protobuf:protoc:3.0.0'
29+
}
30+
plugins {
31+
grpc {
32+
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0-pre2'
33+
}
34+
javalite {
35+
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
36+
}
37+
}
38+
generateProtoTasks {
39+
all()*.plugins {
40+
javalite {}
41+
}
42+
ofNonTest()*.plugins {
43+
grpc {
44+
// Options added to --grpc_out
45+
option 'lite'
46+
}
47+
}
48+
}
49+
}
50+
51+
dependencies {
52+
implementation fileTree(dir: 'libs', include: ['*.jar'])
53+
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
54+
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
55+
testImplementation 'junit:junit:4.12'
56+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
57+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
58+
59+
// GRPC dependencies
60+
implementation 'javax.annotation:javax.annotation-api:1.2'
61+
implementation 'io.grpc:grpc-protobuf-lite:1.12.0'
62+
implementation 'io.grpc:grpc-okhttp:1.12.0'
63+
implementation 'io.grpc:grpc-stub:1.12.0'
64+
65+
// rx java
66+
implementation "io.reactivex.rxjava2:rxjava:$rx_java_verstion"
67+
implementation "io.reactivex.rxjava2:rxandroid:$rx_android"
68+
69+
// dialog library
70+
implementation 'com.github.d-max:spots-dialog:0.7@aar'
71+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.grpc.client;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.example.grpc.client", appContext.getPackageName());
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.grpc.client">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity android:name=".MainActivity">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.example.grpc.client;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
import android.view.View;
6+
import android.widget.Button;
7+
import android.widget.TextView;
8+
9+
import com.example.learn.GreetingServiceGrpc;
10+
import com.example.learn.HelloRequest;
11+
import com.example.learn.HelloResponse;
12+
import com.example.learn.Sentiment;
13+
14+
import dmax.dialog.SpotsDialog;
15+
import io.grpc.ManagedChannel;
16+
import io.grpc.ManagedChannelBuilder;
17+
18+
/**
19+
* := created by: Shuza
20+
* := create date: 10-Jul-2018
21+
* := (C) CopyRight Shuza
22+
* := www.shuza.ninja
23+
* := shuza.sa@gmail.com
24+
* := Fun : Coffee : Code
25+
**/
26+
27+
public class MainActivity extends AppCompatActivity {
28+
29+
private TextView tvResult;
30+
private Button btnConnect;
31+
32+
private final String SERVER_ADDRESS = "192.168.43.169";
33+
private final int PORT = 8080;
34+
35+
private SpotsDialog dialog;
36+
37+
@Override
38+
protected void onCreate(Bundle savedInstanceState) {
39+
super.onCreate(savedInstanceState);
40+
setContentView(R.layout.activity_main);
41+
42+
findViews();
43+
44+
dialog = new SpotsDialog(this, R.style.LoadingDialog);
45+
dialog.setCancelable(false);
46+
47+
btnConnect.setOnClickListener((View v) -> {
48+
ManagedChannel channel = ManagedChannelBuilder.forAddress(SERVER_ADDRESS, PORT)
49+
.usePlaintext()
50+
.build();
51+
52+
requestExampleService(channel);
53+
});
54+
}
55+
56+
private void requestExampleService(ManagedChannel channel) {
57+
GreetingServiceGrpc.GreetingServiceBlockingStub stub = GreetingServiceGrpc
58+
.newBlockingStub(channel);
59+
60+
HelloRequest helloRequest = HelloRequest.newBuilder()
61+
.setName("Shuza")
62+
.setAge(20)
63+
.setSentiment(Sentiment.HAPPY)
64+
.build();
65+
66+
dialog.show();
67+
68+
Thread backgroundThread = new Thread(() -> {
69+
try {
70+
HelloResponse helloResponse = stub.greeting(helloRequest);
71+
showResultMessage("Response: " + helloResponse.getGreeting());
72+
} catch (Exception e) {
73+
showResultMessage("Error: " + e.getMessage());
74+
}
75+
});
76+
77+
78+
backgroundThread.start();
79+
80+
}
81+
82+
/**
83+
* show message on UI Thread
84+
* and dismiss loading dialog
85+
*
86+
* @param message
87+
*/
88+
private void showResultMessage(String message) {
89+
runOnUiThread(() -> {
90+
tvResult.setText(message);
91+
dialog.dismiss();
92+
});
93+
}
94+
95+
/**
96+
* find all components from view
97+
*/
98+
private void findViews() {
99+
tvResult = findViewById(R.id.tv_result);
100+
btnConnect = findViewById(R.id.btn_server_connect);
101+
}
102+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* := created by: Shuza
3+
* := create date: {DATE}
4+
* := (C) CopyRight Shuza
5+
* := www.shuza.ninja
6+
* := shuza.sa@gmail.com
7+
* := Fun : Coffee : Code
8+
**/
9+
10+
syntax = "proto3";
11+
12+
option java_multiple_files = true;
13+
option java_generic_services = true;
14+
package com.example.learn;
15+
16+
enum Sentiment {
17+
HAPPY = 0;
18+
SLEEPY = 1;
19+
ANGRY = 2;
20+
}
21+
22+
message HelloRequest {
23+
string name = 1;
24+
int32 age = 2;
25+
repeated string hobbies = 3;
26+
map<string, string> bagOfTricks = 4;
27+
Sentiment sentiment = 5;
28+
}
29+
30+
message HelloResponse {
31+
string greeting = 1;
32+
}
33+
34+
// 4. service, unary request/response
35+
service GreetingService {
36+
rpc greeting (HelloRequest) returns (HelloResponse);
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1" />
34+
</vector>

0 commit comments

Comments
 (0)