Skip to content

Commit

Permalink
Merge pull request #478 from Paynehe/master
Browse files Browse the repository at this point in the history
#6 #66  第六次作业
  • Loading branch information
zengsn authored Jun 17, 2017
2 parents 1e2ea18 + 058879e commit b0f626b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 20 deletions.
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");//显示解析结果
}
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903103;

import android.content.Intent;
import android.widget.Button;
import android.webkit.WebView;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
Expand All @@ -16,6 +18,8 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.TextView;


public class Net1414080903103Activity extends Activity implements OnClickListener {
private static final String TAG = "自由浏览app";
Expand All @@ -26,11 +30,16 @@ public class Net1414080903103Activity extends Activity implements OnClickListene
private final String VPN_USERNAME = "oa";
private final String VPN_PASSWORD = "123456";
private Dialog mShowingDialog;
private Button send;
private TextView responseText;
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net1414080903103);
findviewByid();
send=(Button)findViewById(R.id.send_request);
send.setOnClickListener(this);
}

private void findviewByid() {
Expand All @@ -40,11 +49,11 @@ private void findviewByid() {
Button btnStart = (Button) findViewById(R.id.BTN_START);
Button btnStop = (Button) findViewById(R.id.BTN_STOP);
Button btnConnectSvr = (Button) findViewById(R.id.BTN_GETVPNSTATUS);
Button btn_save=(Button) findViewById(R.id.btn_save);

btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
btnConnectSvr.setOnClickListener(this);
btn_save.setOnClickListener(this);

}

@Override
Expand All @@ -58,26 +67,16 @@ public void onClick(View v) {
}

case R.id.BTN_STOP: {
doStop();
//doStop();

break;
}

case R.id.btn_save: {
String saveinfo =edtIP.getText().toString().trim();
FileOutputStream fos;
try {
//保存数据
fos = openFileOutput("data.txt", Context.MODE_APPEND);
fos.write(saveinfo.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(TestSVSDKLib.this, "保存成功", Toast.LENGTH_SHORT).show();
break;

}
case R.id.send_request:{
Intent intent3=new Intent(Net1414080903103Activity.this,Net1414080903103_AnalysisJsonActivity.class);
startActivity(intent3);
}

}
}

Expand Down Expand Up @@ -138,7 +137,7 @@ private void doStart() {
return;
}

// vpnlib.setVPNInfo("192.168.95.84", 443, "1", "111111");暂时不懂这部分内容
/* vpnlib.setVPNInfo("192.168.95.84", 443, "1", "111111");暂时不懂这部分内容
vpnlib.setVPNInfo(sIP, port, sUname, sUpwd);
Log.i("ttt", "ip= " + sIP + " port= " + port + " uname= " + sUname);
vpnlib.prepareVPNSettings();
Expand Down Expand Up @@ -178,4 +177,7 @@ private void doGetVPNStatus() { //暂时不懂这部分内容
}
}
*/
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"ip":"192.168.1.1","port":"8080","password":"141408903103"}]
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save VPN INFO"/>
<Button
android:id="@+id/send_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="ץȡJson" />
</LinearLayout>

</LinearLayout>
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>

0 comments on commit b0f626b

Please sign in to comment.