Skip to content

Commit 9cd7e89

Browse files
committed
hzuapps#6 hzuapps#35 第六次作业
1 parent 271d899 commit 9cd7e89

10 files changed

+114
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"grade":"14网1","name":"刘豪杰","number":"1414080903101"}]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903101;
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 Net1414080903101JsonActivity 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_net1414080903101_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/duguhaojie/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903101/1414080903101.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+
}

AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903101/Net1414080903101MainActivity.java

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class Net1414080903101MainActivity extends AppCompatActivity {
1616
TextView tv6;
1717
TextView tv7;
1818
TextView tv8;
19+
Button send;
1920

2021
@Override
2122
protected void onCreate(Bundle savedInstanceState) {
@@ -29,6 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {
2930
tv6= (TextView) findViewById(R.id.tv6);
3031
tv7= (TextView) findViewById(R.id.tv7);
3132
tv8= (TextView) findViewById(R.id.tv8);
33+
send=(Button)findViewById(R.id.send_request);
3234

3335
tv1.setOnClickListener(new View.OnClickListener() {
3436
@Override
@@ -93,6 +95,15 @@ public void onClick(View v) {
9395
intent.putExtra("type",8);
9496
startActivity(intent);
9597
}
98+
});
99+
/*点击按钮解析到github解析json文件*/
100+
send_request.setOnClickListener(new View.OnClickListener() {
101+
@Override
102+
public void onClick(View v) {
103+
Intent intent=new Intent(Net1414080903101MainActivity.this,Net1414080903101JsonActivity.class);
104+
intent.putExtra("type",8);
105+
startActivity(intent);
106+
}
96107
});
97108
}
98109
}

AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903101/activity_net1414080903101_loginActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.intelligent_chest.activity;
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903101;
22

33
import android.app.Activity;
44
import android.content.Intent;

AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903101/activity_net1414080903101_registerActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.intelligent_chest.activity;
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903101;
22

33
import android.app.Activity;
44
import android.app.AlertDialog;

AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903101/db1414080903101_DBOpenHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.intelligent_chest.db;
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903101;
22

33
import android.content.Context;
44
import android.database.sqlite.SQLiteDatabase;

AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903101/db1414080903101_SQLOperate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.intelligent_chest.db;
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903101;
22

33
import com.intelligent_chest.entity.User;
44

AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903101/db1414080903101_SQLOperateImpl.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package com.intelligent_chest.db;
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903101;
22

33

44
import android.content.Context;
55
import android.database.Cursor;
66
import android.database.sqlite.SQLiteDatabase;
77

8-
import com.intelligent_chest.entity.User;
98

109
/**
1110
* Created by Czd on 2017/6/1.
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_net1414080903101_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.net1414080903101.Net1414080903101_AnalysisJsonActivity">
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>

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

+6
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,10 @@
9494
android:textSize="18sp"
9595
android:padding="15dp"
9696
android:text="4.陈式太极单刀"/>
97+
<Button
98+
android:id="@+id/send_request"
99+
android:layout_width="wrap_content"
100+
android:layout_height="wrap_content"
101+
android:layout_alignParentBottom="true"
102+
android:text="抓取Json文件" />
97103
</LinearLayout>

0 commit comments

Comments
 (0)