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 #1341 第六次实验 #2651

Merged
merged 3 commits into from
Jun 2, 2018
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
@@ -0,0 +1,94 @@
package edu.hzuapps.androidlabs.com1614080901138;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class jsonActivity extends AppCompatActivity {
private String text;
private ListView listView;
private ArrayAdapter<String> adapter;
private List<String> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
listView=(ListView)findViewById(R.id.listview);
getjson();
}
public void getjson()
{
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("https://raw.githubusercontent.com/1614080901144/android-labs-2018/master/com1614080901144/content.json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setUseCaches(false);
connection.connect();
InputStream in = connection.getInputStream();
InputStreamReader input=new InputStreamReader(in);
BufferedReader reader=new BufferedReader(input);
if (connection.getResponseCode() == 200) {
StringBuilder response = new StringBuilder();
String Line;
while ((Line = reader.readLine()) != null) {
response.append(Line);
}
text = response.toString();
Log.v("out---------------->",text);
analyzejson();
}
}
catch(Exception e)
{
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override public void run() {
adapter=new ArrayAdapter<String>(jsonActivity.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
});
}
}).start();
}
public void analyzejson()
{
try{
JSONArray jsonArray=new JSONArray(text);
for(int a=0;a<jsonArray.length();a++)
{
JSONObject jsonObject=jsonArray.getJSONObject(a);
String number=jsonObject.getString("number");
String number1=jsonObject.getString("number1");
String number2=jsonObject.getString("number2");
String add;
add=number+" "+number1+" "+number2;
list.add(add);
Log.v("结果",add);
}

}
catch (Exception e)
{
e.printStackTrace();
}
}
}
13 changes: 13 additions & 0 deletions com1614080901138/app/src/main/res/layout/activity_json.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.hzuapps.androidlabs.com1614080901138.jsonActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>

</android.support.constraint.ConstraintLayout>
43 changes: 43 additions & 0 deletions com1614080901138/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 第6次实验报告

## 一.实验目标

(1)掌握Android网络访问方法;

(2)理解XML和JSON表示数据的方法。

## 二.实验内容(选做了第一个)

(1)在个人目录中创建一个表示数据的XML或JSON文件;

(2)数据文件代码提交之后从GitHub获取文件URL;

(3)在应用中通过网络编程访问GitHub的数据文件;

(4)在应用中解析并显示文件所包含的数据;

(5)将应用运行结果截图。

## 三.实验步骤

(1)通过网上的json在线编辑器,然后编写一个json文件,编写完下载到本地。

(2)然后把json文件提交到自己github上的库中

(3)然后在Android studio 上创建一个新的Activity,从github获取json文件URL,再通过BufferedReader对服务器的流进行读取,再调用函数进行json解析。

(4)在AndroidManifest.xml添加对应代码获取网络权限

(6)运行程序并截图,使用git将代码和截图提交到自己的库里

(7)在GitHub中使用Markdown文件编写实验报告(report6.md)

(8)create new pull request

## 3.实验结果

![image](https://github.com/JuneLee1212/android-labs-2018/blob/master/com1614080901138/%E7%AC%AC%E5%85%AD%E6%AC%A1%E5%AE%9E%E9%AA%8C%E6%88%AA%E5%9B%BE1.jpg)
![image](https://github.com/JuneLee1212/android-labs-2018/blob/master/com1614080901138/%E7%AC%AC%E5%85%AD%E6%AC%A1%E5%AE%9E%E9%AA%8C%E6%88%AA%E5%9B%BE2.jpg)
## 4.实验体会
这个实验主要考验我们对网络权限的理解,和对json解析的理解,通过这次实验,我把json解析这方面的内容了解了一遍,加深了我的印象。而实验过程遇到的问题就是json文件的格式没写好,
导致无法获取,出现了403错误,最后解决之后,就可以运行了。
Binary file added com1614080901138/第六次实验截图1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added com1614080901138/第六次实验截图2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.