Skip to content

Commit 929575a

Browse files
committed
Add JavaNet demo
1 parent c8a6175 commit 929575a

File tree

24 files changed

+305
-1
lines changed

24 files changed

+305
-1
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaNet/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

JavaNet/build.gradle

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 26
5+
buildToolsVersion "26.0.1"
6+
7+
defaultConfig {
8+
applicationId "com.luocj.android.javanet"
9+
minSdkVersion 23
10+
targetSdkVersion 26
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
16+
}
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
}
24+
25+
dependencies {
26+
compile fileTree(dir: 'libs', include: ['*.jar'])
27+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
28+
exclude group: 'com.android.support', module: 'support-annotations'
29+
})
30+
compile 'com.android.support:appcompat-v7:26.+'
31+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
32+
testCompile 'junit:junit:4.12'
33+
}

JavaNet/proguard-rules.pro

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/luocj/Android/Sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.luocj.android.javanet;
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+
* Instrumentation 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() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.luocj.android.javanet", appContext.getPackageName());
25+
}
26+
}

JavaNet/src/main/AndroidManifest.xml

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.luocj.android.javanet">
4+
5+
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
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=".JavaNetActivity">
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: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.luocj.android.javanet;
2+
3+
import android.content.pm.PackageManager;
4+
import android.os.Handler;
5+
import android.os.Message;
6+
import android.os.Parcel;
7+
import android.support.annotation.NonNull;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.os.Bundle;
10+
import android.util.Log;
11+
import android.view.View;
12+
import android.widget.Button;
13+
import android.widget.TextView;
14+
import android.widget.Toast;
15+
16+
import java.io.BufferedReader;
17+
import java.io.InputStreamReader;
18+
import java.net.URL;
19+
import java.net.URLConnection;
20+
21+
import static android.Manifest.permission.INTERNET;
22+
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
23+
24+
public class JavaNetActivity extends AppCompatActivity {
25+
26+
private static final String TAG = JavaNetActivity.class.getSimpleName();
27+
28+
private final int PERMISSION_REQUEST_CODE_HTTPAPACHE = 1;
29+
private final int MESSAGE_NETWORK_CONNECT_RESULT = 1;
30+
31+
private JavaNetThread mThread;
32+
33+
private Button mButton;
34+
private TextView mTextView1;
35+
private TextView mTextView2;
36+
37+
@Override
38+
protected void onCreate(Bundle savedInstanceState) {
39+
super.onCreate(savedInstanceState);
40+
setContentView(R.layout.activity_java_net);
41+
42+
43+
44+
mButton = (Button) findViewById(R.id.button1);
45+
mTextView1 = (TextView) findViewById(R.id.editText1);
46+
mTextView2 = (TextView) findViewById(R.id.editText2);
47+
48+
mButton.setOnClickListener(new View.OnClickListener() {
49+
@Override
50+
public void onClick(View view) {
51+
initPermissions();
52+
}
53+
});
54+
55+
}
56+
57+
58+
public void initPermissions() {
59+
if (checkCallingOrSelfPermission(INTERNET) == PackageManager.PERMISSION_GRANTED) {
60+
startNetwork();
61+
} else {
62+
requestPermissions(new String[]{READ_EXTERNAL_STORAGE, INTERNET}, PERMISSION_REQUEST_CODE_HTTPAPACHE);
63+
}
64+
}
65+
66+
@Override
67+
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
68+
switch (requestCode) {
69+
case PERMISSION_REQUEST_CODE_HTTPAPACHE:
70+
startNetwork();
71+
break;
72+
default:
73+
break;
74+
}
75+
}
76+
77+
private void startNetwork() {
78+
if (mThread == null) {
79+
mThread = new JavaNetThread();
80+
}
81+
mThread.start();
82+
}
83+
84+
class JavaNetThread extends Thread {
85+
@Override
86+
public void run() {
87+
execute();
88+
}
89+
}
90+
91+
private void execute() {
92+
String ret = "";
93+
try {
94+
URL url = new URL(mTextView1.getText().toString());
95+
URLConnection connection = url.openConnection();
96+
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
97+
98+
String line = "";
99+
while ((line = rd.readLine()) != null) {
100+
ret += line;
101+
}
102+
} catch (Exception e) {
103+
e.printStackTrace();
104+
}
105+
Message message = mHandler.obtainMessage(MESSAGE_NETWORK_CONNECT_RESULT);
106+
107+
Bundle bundle = new Bundle();
108+
bundle.putString("result", ret);
109+
message.setData(bundle);
110+
mHandler.sendMessage(message);
111+
}
112+
113+
private Handler mHandler = new Handler() {
114+
@Override
115+
public void handleMessage(Message msg) {
116+
switch (msg.what) {
117+
case MESSAGE_NETWORK_CONNECT_RESULT:
118+
String result = (String) msg.getData().getString("result");
119+
Toast.makeText(JavaNetActivity.this, "Getnetwork OK "+ result, Toast.LENGTH_LONG).show();
120+
mTextView2.setText("");
121+
mTextView2.setText(result);
122+
}
123+
}
124+
};
125+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
tools:context="com.luocj.android.javanet.JavaNetActivity">
8+
9+
<TextView
10+
android:id="@+id/textview1"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:text="Enter URL!" />
14+
15+
<EditText
16+
android:id="@+id/editText1"
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:text="http://192.168.40.53/mediawiki/index.php/Dolphin-Walrus" />
20+
21+
<Button
22+
android:id="@+id/button1"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:text="Click" />
26+
27+
<EditText
28+
android:id="@+id/editText2"
29+
android:layout_width="match_parent"
30+
android:layout_height="match_parent" />
31+
32+
</LinearLayout>
3.34 KB
Loading
Loading
2.15 KB
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">JavaNet</string>
3+
</resources>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
<item name="colorPrimary">@color/colorPrimary</item>
7+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
8+
<item name="colorAccent">@color/colorAccent</item>
9+
</style>
10+
11+
</resources>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.luocj.android.javanet;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
11+
*/
12+
public class ExampleUnitTest {
13+
@Test
14+
public void addition_isCorrect() throws Exception {
15+
assertEquals(4, 2 + 2);
16+
}
17+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':app', ':HttpApache'
1+
include ':app', ':HttpApache', ':JavaNet'

0 commit comments

Comments
 (0)