-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #481 from 14YuQingBin/master
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...java/edu/hzuapps/androidlabs/homeworks/net1414080903130/Net1414080903130JsonActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package edu.hzuapps.androidlabs.homeworks.net1414080903130; | ||
|
||
import android.os.Handler; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
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; | ||
|
||
public class Net1414080903130JsonActivity 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_net1414080903130_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/14YuQingBin/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903130/yqb.json").build();//目标地址 | ||
Response response = client.newCall(request).execute(); | ||
String responseData = response.body().string(); | ||
ParseJson(responseData); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
handler.post(runnableUi); | ||
} | ||
}.start(); | ||
} | ||
|
||
/*解析json文件*/ | ||
private void ParseJson(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");//显示解析结果 | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903130/yqb.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"grade":"14ÍøÂç1°à","name":"ÓàÇì±ó","number":"1414080903130"}] |
16 changes: 16 additions & 0 deletions
16
AndroidLabs/app/src/main/res/layout/activity_net1414080903130_json.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?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_net1414080903130_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.net1414080903130.Net1414080903130JsonActivity"> | ||
<TextView | ||
android:id="@+id/response_info" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" /> | ||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters