-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #478 from Paynehe/master
- Loading branch information
Showing
5 changed files
with
122 additions
and
20 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...hzuapps/androidlabs/homeworks/net1414080903103/Net1414080903103_AnalysisJsonActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package edu.hzuapps.androidlabs.homeworks.net1414080903103; | ||
|
||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.widget.TextView; | ||
|
||
|
||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
/*抓取并解析json文件*/ | ||
public class Net1414080903103_AnalysisJsonActivity extends AppCompatActivity { | ||
private String ip; | ||
private String port; | ||
private String password; | ||
private TextView GithubResponse; | ||
private Handler handler = null; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_net1414080903103__analysis_json); | ||
handler = new Handler(); | ||
GithubResponse = (TextView) findViewById(R.id.response_info); | ||
sendRequest(); | ||
} | ||
|
||
private void sendRequest() { | ||
new Thread(){ | ||
@Override | ||
public void run() { | ||
try { | ||
OkHttpClient client = new OkHttpClient(); | ||
Request request = new Request.Builder().url("https://raw.githubusercontent.com/Paynehe/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903103/vpn_info.json").build();//目标地址 | ||
Response response = client.newCall(request).execute(); | ||
String responseData = response.body().string(); | ||
AnalysisJson(responseData); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
handler.post(runnableUi); | ||
} | ||
}.start(); | ||
} | ||
|
||
/*解析json文件*/ | ||
private void AnalysisJson(String jsonData) { | ||
try { | ||
JSONArray jsonArray = new JSONArray(jsonData); | ||
for (int i = 0; i < jsonArray.length(); i++) { | ||
JSONObject jsonObject = jsonArray.getJSONObject(i); | ||
ip = jsonObject.getString("ip"); | ||
port = jsonObject.getString("port"); | ||
password = jsonObject.getString("password"); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/*更新UI*/ | ||
Runnable runnableUi = new Runnable() { | ||
public void run() { | ||
GithubResponse.setText("IP" + ip + "\n" + "\n" + "Port" + port + "\n" + "\n" + "Password" + password + "\n" + "\n");//显示解析结果 | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...idLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903103/vpn_info.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"ip":"192.168.1.1","port":"8080","password":"141408903103"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
AndroidLabs/app/src/main/res/layout/activity_net1414080903103__analysis_json.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/activity_net1414080903103__analysis_json" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingBottom="@dimen/activity_vertical_margin" | ||
android:paddingLeft="@dimen/activity_horizontal_margin" | ||
android:paddingRight="@dimen/activity_horizontal_margin" | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
tools:context="edu.hzuapps.androidlabs.homeworks.net1414080903103.Net1414080903103_AnalysisJsonActivity"> | ||
|
||
<!--显示解析结果--> | ||
<TextView | ||
android:id="@+id/response_info" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
|
||
</RelativeLayout> |