Skip to content

Commit c008f19

Browse files
committed
hzuapps#6 hzuapps#178 第六次作业
1 parent 03469b5 commit c008f19

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903109/Net1414080903109MainActivity.java

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ protected void onCreate(Bundle savedInstanceState) {
1919
setContentView(R.layout.activity_net1414080903109_main);
2020
btAdd= (Button) findViewById(R.id.bt_to_add);
2121
btList= (Button) findViewById(R.id.bt_to_list);
22+
btJson=(Button)findViewById(R.id.bt_to_json);
2223

2324
/*点击按钮跳转到添加节日的界面*/
2425
btAdd.setOnClickListener(new View.OnClickListener() {
@@ -34,5 +35,12 @@ public void onClick(View v) {
3435
startActivity(new Intent(Net1414080903109MainActivity.this,Net1414080903109ListActivity.class));
3536
}
3637
});
38+
/*点击按钮跳转到解析Json文件结果界面*/
39+
btJson.setOnClickListener(new View.OnClickListener() {
40+
@Override
41+
public void onClick(View v) {
42+
startActivity(new Intent(Net1414080903109MainActivity.this,Net1414080903109_ParseJsonActivity.class));
43+
}
44+
});
3745
}
3846
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903109;
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 Net1414080903109_ParseJsonActivity 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_net1414080903110_analysis_json);
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/mamingjian/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903109/mamingjian.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 @@
1+
[{"grade":"14网络1班","name":"马明健","number":"1414080903109"}]

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
55
android:orientation="vertical">
6-
7-
/*主界面*/
6+
87
<TextView
98
android:layout_width="wrap_content"
109
android:layout_height="wrap_content"
@@ -42,4 +41,14 @@
4241
android:background="@color/colorPrimary"
4342
android:text="查看节日"
4443
android:textColor="#ffffff" />
44+
<Button
45+
android:id="@+id/bt_to_json"
46+
android:layout_width="match_parent"
47+
android:layout_height="wrap_content"
48+
android:layout_marginLeft="60dp"
49+
android:layout_marginRight="60dp"
50+
android:layout_marginTop="20dp"
51+
android:background="@color/colorPrimary"
52+
android:text="解析Json文件"
53+
android:textColor="#ffffff" />
4554
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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_net1414080903109__parse_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.homeworks.net1414080903109.Net1414080903109_ParseJsonActivity">
12+
13+
<!--显示解析结果-->
14+
<TextView
15+
android:id="@+id/response_info"
16+
android:layout_width="match_parent"
17+
android:layout_height="match_parent" />
18+
19+
</RelativeLayout>

0 commit comments

Comments
 (0)