Skip to content

Commit c8a6175

Browse files
committed
Done HttpApache Demo
1 parent ab149ae commit c8a6175

File tree

9 files changed

+204
-29
lines changed

9 files changed

+204
-29
lines changed

.idea/misc.xml

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

.idea/modules.xml

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

HttpApache/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ apply plugin: 'com.android.application'
33
android {
44
compileSdkVersion 26
55
buildToolsVersion "26.0.1"
6-
76
defaultConfig {
87
applicationId "com.luocj.android.httpapache"
9-
minSdkVersion 21
8+
minSdkVersion 23
109
targetSdkVersion 26
1110
versionCode 1
1211
versionName "1.0"
@@ -20,10 +19,13 @@ android {
2019
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2120
}
2221
}
22+
useLibrary 'org.apache.http.legacy'
23+
productFlavors {
24+
}
2325
}
2426

2527
dependencies {
26-
compile fileTree(dir: 'libs', include: ['*.jar'])
28+
compile fileTree(include: ['*.jar'], dir: 'libs')
2729
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
2830
exclude group: 'com.android.support', module: 'support-annotations'
2931
})

HttpApache/src/main/AndroidManifest.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.luocj.android.httpapache">
44

5-
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
6-
android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
7-
android:supportsRtl="true" android:theme="@style/AppTheme">
5+
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
6+
7+
<application
8+
android:name=".HttpApacheApplication"
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/AppTheme">
815
<activity android:name=".HttpApacheActivity">
916
<intent-filter>
1017
<action android:name="android.intent.action.MAIN" />
Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,124 @@
11
package com.luocj.android.httpapache;
22

3-
import android.support.v7.app.AppCompatActivity;
3+
import android.content.pm.PackageManager;
44
import android.os.Bundle;
5+
import android.os.Handler;
6+
import android.os.Message;
7+
import android.support.annotation.NonNull;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.view.View;
10+
import android.widget.Button;
11+
import android.widget.Toast;
12+
13+
import org.apache.http.HttpResponse;
14+
import org.apache.http.HttpStatus;
15+
import org.apache.http.client.HttpClient;
16+
import org.apache.http.client.methods.HttpGet;
17+
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.InputStream;
20+
21+
import static android.Manifest.permission.INTERNET;
22+
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
523

624
public class HttpApacheActivity extends AppCompatActivity {
725

26+
private final int PERMISSION_REQUEST_CODE_HTTPAPACHE = 1;
27+
28+
private final int MESSAGE_NETWORK_CONNECT_RESULT = 1;
29+
30+
private HttpApacheThread mThread;
31+
832
@Override
933
protected void onCreate(Bundle savedInstanceState) {
1034
super.onCreate(savedInstanceState);
1135
setContentView(R.layout.activity_http_apache);
36+
37+
Button button = (Button) findViewById(R.id.botton);
38+
39+
button.setOnClickListener(new View.OnClickListener() {
40+
@Override
41+
public void onClick(View view) {
42+
initPermissions();
43+
}
44+
});
45+
46+
}
47+
48+
class HttpApacheThread extends Thread {
49+
@Override
50+
public void run() {
51+
execute();
52+
}
53+
}
54+
55+
private Handler mHandler = new Handler() {
56+
@Override
57+
public void handleMessage(Message msg) {
58+
switch (msg.what) {
59+
case MESSAGE_NETWORK_CONNECT_RESULT:
60+
String result = (String) msg.obj;
61+
Toast.makeText(HttpApacheActivity.this, result, Toast.LENGTH_LONG).show();
62+
}
63+
}
64+
};
65+
66+
public void initPermissions() {
67+
if (checkCallingOrSelfPermission(INTERNET) == PackageManager.PERMISSION_GRANTED) {
68+
startNetwork();
69+
} else {
70+
requestPermissions(new String[]{READ_EXTERNAL_STORAGE, INTERNET}, PERMISSION_REQUEST_CODE_HTTPAPACHE);
71+
}
72+
}
73+
74+
@Override
75+
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
76+
switch (requestCode) {
77+
case PERMISSION_REQUEST_CODE_HTTPAPACHE:
78+
startNetwork();
79+
break;
80+
default:
81+
break;
82+
}
83+
}
84+
85+
private void startNetwork() {
86+
if (mThread == null) {
87+
mThread = new HttpApacheThread();
88+
}
89+
mThread.start();
90+
}
91+
92+
private void execute() {
93+
try {
94+
HttpApacheApplication app = (HttpApacheApplication) getApplication();
95+
96+
HttpClient client = app.getHttpClient();
97+
HttpGet get = new HttpGet("http://192.168.1.57:8080/web/TestServlet?id=1001&name=john&age=60");
98+
HttpResponse response = client.execute(get);
99+
100+
String result = "Network connect failed";
101+
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
102+
InputStream is = response.getEntity().getContent();
103+
result = inStream2String(is);
104+
}
105+
106+
Message message = mHandler.obtainMessage(MESSAGE_NETWORK_CONNECT_RESULT);
107+
message.obj = result;
108+
mHandler.sendMessage(message);
109+
110+
} catch (Exception e) {
111+
e.printStackTrace();
112+
}
113+
}
114+
115+
private String inStream2String(InputStream is) throws Exception {
116+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
117+
byte[] buf = new byte[1024];
118+
int len = -1;
119+
while ((len = is.read(buf)) != -1) {
120+
bos.write(buf, 0, len);
121+
}
122+
return new String(bos.toByteArray());
12123
}
13124
}

HttpApache/src/main/java/com/luocj/android/httpapache/HttpApacheApplication.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,69 @@
22

33
import android.app.Application;
44

5+
import org.apache.http.HttpVersion;
56
import org.apache.http.client.HttpClient;
7+
import org.apache.http.conn.ClientConnectionManager;
8+
import org.apache.http.conn.scheme.PlainSocketFactory;
9+
import org.apache.http.conn.scheme.Scheme;
10+
import org.apache.http.conn.scheme.SchemeRegistry;
11+
import org.apache.http.impl.client.DefaultHttpClient;
12+
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
13+
import org.apache.http.params.BasicHttpParams;
14+
import org.apache.http.params.HttpParams;
15+
import org.apache.http.params.HttpProtocolParams;
16+
import org.apache.http.protocol.HTTP;
617

718

819
/**
920
* Created by TS on 2017/7/30.
1021
*/
1122

12-
public class HttpApacheApplication extends Application{
23+
public class HttpApacheApplication extends Application {
1324

1425
private HttpClient mHttpClient;
1526

1627
@Override
1728
public void onCreate() {
1829
super.onCreate();
1930

20-
mHttpClient = creatHeepClient();
31+
mHttpClient = creatHttpClient();
2132
}
2233

2334
@Override
2435
public void onLowMemory() {
2536
super.onLowMemory();
37+
shutdownHttpClient();
2638
}
2739

2840
@Override
2941
public void onTerminate() {
3042
super.onTerminate();
43+
shutdownHttpClient();
44+
}
45+
46+
private HttpClient creatHttpClient() {
47+
HttpParams params = new BasicHttpParams();
48+
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
49+
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
50+
HttpProtocolParams.setUseExpectContinue(params, true);
51+
52+
SchemeRegistry schReg = new SchemeRegistry();
53+
schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
54+
schReg.register(new Scheme("https", PlainSocketFactory.getSocketFactory(), 443));
55+
56+
ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schReg);
57+
58+
return new DefaultHttpClient();
59+
}
60+
61+
private void shutdownHttpClient() {
62+
if (mHttpClient != null && mHttpClient.getConnectionManager() != null) {
63+
mHttpClient.getConnectionManager().shutdown();
64+
}
65+
}
66+
67+
public HttpClient getHttpClient() {
68+
return mHttpClient;
3169
}
3270
}
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.constraint.ConstraintLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:app="http://schemas.android.com/apk/res-auto"
5-
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
66
android:layout_height="match_parent"
77
tools:context="com.luocj.android.httpapache.HttpApacheActivity">
88

9-
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
10-
android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent"
11-
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
9+
<TextView
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:text="Hello World!"
13+
app:layout_constraintBottom_toBottomOf="parent"
14+
app:layout_constraintLeft_toLeftOf="parent"
15+
app:layout_constraintRight_toRightOf="parent"
1216
app:layout_constraintTop_toTopOf="parent" />
1317

18+
<Button
19+
android:id="@+id/botton"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:text="@string/app_name" />
23+
1424
</android.support.constraint.ConstraintLayout>

app/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 26
5-
buildToolsVersion "25.0.3"
5+
buildToolsVersion '26.0.1'
66
defaultConfig {
77
applicationId "com.luocj.android.androidnetworkprogram"
88
minSdkVersion 21
@@ -27,10 +27,12 @@ android {
2727
path "CMakeLists.txt"
2828
}
2929
}
30+
31+
useLibrary 'org.apache.http.legacy'
3032
}
3133

3234
dependencies {
33-
compile fileTree(dir: 'libs', include: ['*.jar'])
35+
compile fileTree(include: ['*.jar'], dir: 'libs')
3436
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
3537
exclude group: 'com.android.support', module: 'support-annotations'
3638
})

gradle.properties

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
# Project-wide Gradle settings.
2-
3-
# IDE (e.g. Android Studio) users:
4-
# Gradle settings configured through the IDE *will override*
5-
# any settings specified in this file.
6-
1+
## Project-wide Gradle settings.
2+
#
73
# For more details on how to configure your build environment visit
84
# http://www.gradle.org/docs/current/userguide/build_environment.html
9-
5+
#
106
# Specifies the JVM arguments used for the daemon process.
117
# The setting is particularly useful for tweaking memory settings.
12-
org.gradle.jvmargs=-Xmx1536m
13-
8+
# Default value: -Xmx1024m -XX:MaxPermSize=256m
9+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10+
#
1411
# When configured, Gradle will run in incubating parallel mode.
1512
# This option should only be used with decoupled projects. More details, visit
1613
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1714
# org.gradle.parallel=true
15+
#Mon Jul 31 13:29:58 CST 2017
16+
systemProp.http.proxyPort=3128
17+
systemProp.http.proxyUser=luocj0613
18+
systemProp.http.proxyPassword=LUOcj-888
19+
org.gradle.jvmargs=-Xmx1536m
20+
systemProp.https.proxyPassword=LUOcj-888
21+
systemProp.https.proxyHost=192.168.67.10
22+
systemProp.http.proxyHost=192.168.67.10
23+
systemProp.https.proxyPort=3128
24+
systemProp.https.proxyUser=luocj0613

0 commit comments

Comments
 (0)