Skip to content

Commit

Permalink
Merge pull request #481 from 14YuQingBin/master
Browse files Browse the repository at this point in the history
#6 #129 第六次作业
  • Loading branch information
zengsn authored Jun 17, 2017
2 parents e4e6ab5 + c2c1a7a commit 8d5dffc
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
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");//显示解析结果
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public void onClick(View v) {

}
});
Button c = (Button)findViewById(R.id.json_login);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Net1414080903130LoginActivity.this,Net1414080903130JsonActivity.class);
startActivity(intent);
}
});
}

public int check(String username,String password){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"grade":"14ÍøÂç1°à","name":"ÓàÇì±ó","number":"1414080903130"}]
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>
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,11 @@
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="登录" />
<Button
android:id="@+id/json_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="解析json" />
</LinearLayout>
</RelativeLayout>

0 comments on commit 8d5dffc

Please sign in to comment.