Skip to content

Commit 062bc8a

Browse files
authored
Merge pull request #488 from pengliangjun/master
#6 #39 第六次作业
2 parents 2e345ec + 9187793 commit 062bc8a

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1409081602222/Net1409081602222MenuActivity.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ public class Net1409081602222MenuActivity extends AppCompatActivity {
1212

1313
Button btBudget;
1414
Button btCertificate;
15+
Button btJson;
1516

1617
@Override
1718
protected void onCreate(Bundle savedInstanceState) {
1819
super.onCreate(savedInstanceState);
1920
setContentView(R.layout.activity_menu_net1409081602222);
2021
btBudget= (Button) findViewById(R.id.bt_to_apply_budget);
2122
btCertificate= (Button) findViewById(R.id.bt_to_apply_certificate);
22-
23+
btJson=(Button)findViewById(R.id.bt_json);
2324
btBudget.setOnClickListener(new View.OnClickListener() {
2425
@Override
2526
public void onClick(View v) {
@@ -33,6 +34,12 @@ public void onClick(View v) {
3334
public void onClick(View v) {
3435
startActivity(new Intent(Net1409081602222MenuActivity.this,Net1409081602222ApplyCertificateActivity.class));
3536
}
37+
});
38+
btJson.setOnClickListener(new View.OnClickListener() {
39+
@Override
40+
public void onClick(View v) {
41+
startActivity(new Intent(Net1409081602222MenuActivity.this,Net1409081602222_JsonActivity.class));
42+
}
3643
});
3744
}
3845
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903110;
2+
3+
import android.os.Bundle;
4+
import android.os.Handler;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.widget.TextView;
7+
8+
import org.json.JSONArray;
9+
import org.json.JSONObject;
10+
11+
import edu.hzuapps.androidlabs.R;
12+
import okhttp3.OkHttpClient;
13+
import okhttp3.Request;
14+
import okhttp3.Response;
15+
16+
/*抓取并解析json文件*/
17+
public class Net1414080903110_AnalysisJsonActivity extends AppCompatActivity {
18+
private String grade;
19+
private String name;
20+
private String number;
21+
private TextView GithubResponse;
22+
private Handler handler = null;
23+
24+
@Override
25+
protected void onCreate(Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState);
27+
setContentView(R.layout.activity_net1409081602222json);
28+
handler = new Handler();
29+
GithubResponse = (TextView) findViewById(R.id.response_info);
30+
sendRequest();
31+
}
32+
33+
private void sendRequest() {
34+
new Thread(){
35+
@Override
36+
public void run() {
37+
try {
38+
OkHttpClient client = new OkHttpClient();
39+
Request request = new Request.Builder().url("https://raw.githubusercontent.com/pengliangjun/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1409081602222/stu_info.json").build();//目标地址
40+
Response response = client.newCall(request).execute();
41+
String responseData = response.body().string();
42+
AnalysisJson(responseData);
43+
} catch (Exception e) {
44+
e.printStackTrace();
45+
}
46+
handler.post(runnableUi);
47+
}
48+
}.start();
49+
}
50+
51+
/*解析json文件*/
52+
private void AnalysisJson(String jsonData) {
53+
try {
54+
JSONArray jsonArray = new JSONArray(jsonData);
55+
for (int i = 0; i < jsonArray.length(); i++) {
56+
JSONObject jsonObject = jsonArray.getJSONObject(i);
57+
grade = jsonObject.getString("grade");
58+
name = jsonObject.getString("name");
59+
number = jsonObject.getString("number");
60+
}
61+
} catch (Exception e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
66+
/*更新UI*/
67+
Runnable runnableUi = new Runnable() {
68+
public void run() {
69+
GithubResponse.setText("班级: " + grade + "\n" + "\n" + "姓名: " + name + "\n" + "\n" + "学号: " + number + "\n" + "\n");//显示解析结果
70+
}
71+
};
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[{"grade":"14网络1班","name":"彭亮钧","number":"1409081602222"}]
2+
//requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉标题

AndroidLabs/app/src/main/res/layout/activity_menu_net1409081602222.xml

+11
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,16 @@
3939
android:text="申请安全证书"
4040
android:textSize="18sp"
4141
android:background="@color/colorPrimary"/>
42+
<Button
43+
android:id="@+id/bt_Json"
44+
android:layout_marginLeft="50dp"
45+
android:layout_marginRight="50dp"
46+
android:layout_marginTop="25dp"
47+
android:layout_width="match_parent"
48+
android:layout_height="wrap_content"
49+
android:textColor="#ffffff"
50+
android:text="解析json"
51+
android:textSize="18sp"
52+
android:background="@color/colorPrimary"/>
4253

4354
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@+id/activity_net1414080903137_json"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:paddingBottom="@dimen/activity_vertical_margin"
8+
android:paddingLeft="@dimen/activity_horizontal_margin"
9+
android:paddingRight="@dimen/activity_horizontal_margin"
10+
android:paddingTop="@dimen/activity_vertical_margin"
11+
tools:context="edu.hzuapps.androidlabs.homworks.net1414080903137.Net1414080903137JsonActivity">
12+
13+
<TextView
14+
android:text=""
15+
android:layout_width="wrap_content"
16+
android:layout_height="wrap_content"
17+
android:id="@+id/GithubResponse" />
18+
</RelativeLayout>

0 commit comments

Comments
 (0)