Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 #39 第六次作业 #488

Merged
merged 2 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ public class Net1409081602222MenuActivity extends AppCompatActivity {

Button btBudget;
Button btCertificate;
Button btJson;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_net1409081602222);
btBudget= (Button) findViewById(R.id.bt_to_apply_budget);
btCertificate= (Button) findViewById(R.id.bt_to_apply_certificate);

btJson=(Button)findViewById(R.id.bt_json);
btBudget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -33,6 +34,12 @@ public void onClick(View v) {
public void onClick(View v) {
startActivity(new Intent(Net1409081602222MenuActivity.this,Net1409081602222ApplyCertificateActivity.class));
}
});
btJson.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Net1409081602222MenuActivity.this,Net1409081602222_JsonActivity.class));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903110;

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 edu.hzuapps.androidlabs.R;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/*抓取并解析json文件*/
public class Net1414080903110_AnalysisJsonActivity extends AppCompatActivity {
private String grade;
private String name;
private String number;
private TextView GithubResponse;
private Handler handler = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net1409081602222json);
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/pengliangjun/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1409081602222/stu_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);
grade = jsonObject.getString("grade");
name = jsonObject.getString("name");
number = jsonObject.getString("number");
}
} catch (Exception e) {
e.printStackTrace();
}
}

/*更新UI*/
Runnable runnableUi = new Runnable() {
public void run() {
GithubResponse.setText("班级: " + grade + "\n" + "\n" + "姓名: " + name + "\n" + "\n" + "学号: " + number + "\n" + "\n");//显示解析结果
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[{"grade":"14网络1班","name":"彭亮钧","number":"1409081602222"}]
//requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉标题
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,16 @@
android:text="申请安全证书"
android:textSize="18sp"
android:background="@color/colorPrimary"/>
<Button
android:id="@+id/bt_Json"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="解析json"
android:textSize="18sp"
android:background="@color/colorPrimary"/>

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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_net1414080903137_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.homworks.net1414080903137.Net1414080903137JsonActivity">

<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/GithubResponse" />
</RelativeLayout>