|
| 1 | +package edu.hzuapps.androidlabs.homeworks.net1414080903117; |
| 2 | + |
| 3 | +import android.app.DownloadManager; |
| 4 | +import android.os.Handler; |
| 5 | +import android.support.v7.app.AppCompatActivity; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.widget.TextView; |
| 8 | + |
| 9 | +import org.json.JSONArray; |
| 10 | +import org.json.JSONObject; |
| 11 | + |
| 12 | +import edu.hzuapps.androidlabs.R; |
| 13 | +import okhttp3.OkHttpClient; |
| 14 | +import okhttp3.Request; |
| 15 | +import okhttp3.Response; |
| 16 | + |
| 17 | +public class Net1414080903117ParseJsonActivity extends AppCompatActivity { |
| 18 | + private String floor; |
| 19 | + private String dorm; |
| 20 | + private String water; |
| 21 | + private String electric; |
| 22 | + private String info; |
| 23 | + private TextView GithubResponse; |
| 24 | + private Handler handler = null; |
| 25 | + @Override |
| 26 | + protected void onCreate(Bundle savedInstanceState) { |
| 27 | + super.onCreate(savedInstanceState); |
| 28 | + setContentView(R.layout.activity_net1414080903117_parse_json); |
| 29 | + handler = new Handler(); |
| 30 | + GithubResponse = (TextView) findViewById(R.id.json_info); |
| 31 | + sendRequest(); |
| 32 | + } |
| 33 | + |
| 34 | + private void sendRequest() { |
| 35 | + new Thread(){ |
| 36 | + @Override |
| 37 | + public void run() { |
| 38 | + try { |
| 39 | + OkHttpClient client = new OkHttpClient(); |
| 40 | + Request request = new Request.Builder().url("https://raw.githubusercontent.com/JCLin0318/android-labs-2017/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903117/1414080903117.json").build();//目标地址 |
| 41 | + Response response = client.newCall(request).execute(); |
| 42 | + String responseData = response.body().string(); |
| 43 | + ParseJson(responseData); |
| 44 | + } catch (Exception e) { |
| 45 | + e.printStackTrace(); |
| 46 | + } |
| 47 | + handler.post(runnableUi); |
| 48 | + } |
| 49 | + }.start(); |
| 50 | + } |
| 51 | + |
| 52 | + /*解析json文件*/ |
| 53 | + private void ParseJson(String jsonData) { |
| 54 | + try { |
| 55 | + JSONArray jsonArray = new JSONArray(jsonData); |
| 56 | + for (int i = 0; i < jsonArray.length(); i++) { |
| 57 | + JSONObject jsonObject = jsonArray.getJSONObject(i); |
| 58 | + floor= jsonObject.getString("floor"); |
| 59 | + dorm= jsonObject.getString("dorm"); |
| 60 | + water= jsonObject.getString("water"); |
| 61 | + electric= jsonObject.getString("electric"); |
| 62 | + info= jsonObject.getString("info"); |
| 63 | + } |
| 64 | + } catch (Exception e) { |
| 65 | + e.printStackTrace(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /*更新UI*/ |
| 70 | + Runnable runnableUi = new Runnable() { |
| 71 | + public void run() { |
| 72 | + GithubResponse.setText("宿舍楼号: " + floor + "\n" + "\n" + "宿舍号: " + dorm + "\n" + "\n" + "水费欠费金额: " + water + "\n" + "\n"+ "电费欠费金额: " + electric + "\n" + "\n"+ "通知: " + info + "\n" + "\n" |
| 73 | + |
| 74 | + );//显示解析结果 |
| 75 | + } |
| 76 | + }; |
| 77 | +} |
0 commit comments