Skip to content

Commit

Permalink
hzuapps#6 hzuapps#35 第六次作业
Browse files Browse the repository at this point in the history
  • Loading branch information
duguhaojie committed Jun 24, 2017
1 parent 271d899 commit 9cd7e89
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"grade":"14网1","name":"刘豪杰","number":"1414080903101"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903101;

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 Net1414080903101JsonActivity 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_net1414080903101_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/duguhaojie/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903101/1414080903101.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
Expand Up @@ -16,6 +16,7 @@ public class Net1414080903101MainActivity extends AppCompatActivity {
TextView tv6;
TextView tv7;
TextView tv8;
Button send;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -29,6 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {
tv6= (TextView) findViewById(R.id.tv6);
tv7= (TextView) findViewById(R.id.tv7);
tv8= (TextView) findViewById(R.id.tv8);
send=(Button)findViewById(R.id.send_request);

tv1.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -93,6 +95,15 @@ public void onClick(View v) {
intent.putExtra("type",8);
startActivity(intent);
}
});
/*点击按钮解析到github解析json文件*/
send_request.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Net1414080903101MainActivity.this,Net1414080903101JsonActivity.class);
intent.putExtra("type",8);
startActivity(intent);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.intelligent_chest.activity;
package edu.hzuapps.androidlabs.homeworks.net1414080903101;

import android.app.Activity;
import android.content.Intent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.intelligent_chest.activity;
package edu.hzuapps.androidlabs.homeworks.net1414080903101;

import android.app.Activity;
import android.app.AlertDialog;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.intelligent_chest.db;
package edu.hzuapps.androidlabs.homeworks.net1414080903101;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.intelligent_chest.db;
package edu.hzuapps.androidlabs.homeworks.net1414080903101;

import com.intelligent_chest.entity.User;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.intelligent_chest.db;
package edu.hzuapps.androidlabs.homeworks.net1414080903101;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.intelligent_chest.entity.User;

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

<!--ÏÔʾ½âÎö½á¹û-->
<TextView
android:id="@+id/response_info"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@
android:textSize="18sp"
android:padding="15dp"
android:text="4.陈式太极单刀"/>
<Button
android:id="@+id/send_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="抓取Json文件" />
</LinearLayout>

0 comments on commit 9cd7e89

Please sign in to comment.