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 #1094 231第六次实验 #2215

Merged
merged 13 commits into from
May 24, 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
16 changes: 11 additions & 5 deletions soft1614080902231/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.soft1614080902231">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -10,16 +15,17 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Soft1614080902231Activity">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecActivity">

</activity>
<activity android:name=".SecActivity"></activity>
<activity
android:name=".ThreeActivity"
android:label="@string/title_activity_three"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.example.administrator.soft1614080902231;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 ThreeActivity extends AppCompatActivity {
private ArrayAdapter<String> adapter1;
private ListView listView;
private String text;
private List<String> list=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
init();
getJson();
}
public void init() {
listView=(ListView)findViewById(R.id.zhongchao);
}
public void getJson() {
new Thread() {
@Override
public void run() {
try {
//你的URL
String url_s = "https://raw.githubusercontent.com/zglx/android-labs-2018/master/soft1614080902231/informatian.json";
URL url = new URL(url_s);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setUseCaches(false);
conn.connect();
InputStream inputStream = conn.getInputStream();
InputStreamReader input = new InputStreamReader(inputStream);
BufferedReader buffer = new BufferedReader(input);
if (conn.getResponseCode() == 200) {
String inputLine;
StringBuffer resultData = new StringBuffer();
while ((inputLine = buffer.readLine()) != null) {
resultData.append(inputLine);
}
text = resultData.toString();
Log.v("out---------------->", text);
parseJson();
}
} catch (Exception e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter1 = new ArrayAdapter<String>(
ThreeActivity.this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter1);
}
});
}
}.start();
}

public void parseJson() {
try {
JSONArray jsonArray = new JSONArray(text);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject person = jsonArray.getJSONObject(i);
String home = person.getString("home team");
String goal1 = person.getString("goal1");
String visit = person.getString("visiting team");
String goal2 = person.getString("goal2");
String msg;
msg= home+" "+goal1+" : "+goal2+" "+visit;
Log.v("结果",msg);
list.add(msg);
}
} catch (Exception e) {
Log.v("出错","Worring");
e.printStackTrace();
}
}

}
33 changes: 33 additions & 0 deletions soft1614080902231/app/src/main/res/layout/activity_three.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ThreeActivity">

<TextView
android:layout_width="194dp"
android:layout_height="106dp"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="主队"
android:textSize="50dp"/>

<TextView
android:layout_width="192dp"
android:layout_height="104dp"
android:layout_marginLeft="180dp"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="客队 "
android:textSize="50dp" />


<ListView
android:id="@+id/zhongchao"
android:layout_width="match_parent"
android:layout_height="461dp"
android:layout_alignParentBottom="true"
android:textSize="50dp" />
</RelativeLayout>
5 changes: 5 additions & 0 deletions soft1614080902231/informatian.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[{"home team":"广州恒大","goal1":"2","visiting team":"江苏苏宁","goal2":"1"},
{"home team":"上海上港","goal1":"3","visiting team":"北京人和","goal2":"1"},
{"home team":"广州富力","goal1":"0","visiting team":"天津权健","goal2":"0"},
{"home team":"长春亚泰","goal1":"0","visiting team":"北京国安","goal2":"1"},
{"home team":"天津泰达","goal1":"1","visiting team":"河南建业","goal2":"1"}]
Binary file added soft1614080902231/r6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions soft1614080902231/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
实验六
=
一.实验目的
-
掌握Android网络访问方法<br>

二.实验内容
-
在个人目录中创建一个表示数据的XML或JSON文件<br>
数据文件代码提交之后从GitHub获取文件URL<br>
在应用中通过网络编程访问GitHub的数据文件<br>
在应用中解析并显示文件所包含的数据;<br>
将应用运行结果截图。<br>

三、实验步骤
-
1、在自己的Github库中创建一个JSON数据文件<br>
2、在子线程中新建一个URL对象,使用url.openConnection()去访问URL,获得请求<br>
3、使用输入流去读取请求的数据(也就是JSON的数据类型转换成String)<br>
4、解析JSON,通过键取得对应的值,添加到数据类型ArrayList中<br>
5、返回主线程去更新UI<br>

四、实验截图
-
![](https://github.com/zglx/android-labs-2018/blob/master/soft1614080902231/r6.png)

四、实验体会
-
这次实验获取JSON和解析JSON方法不难,就是更新UI有点困难,因为刚开始不知道子线程如果用来更新UI会造成程序终止,百度后,使用 runOnUiThread返回主线程后就可以了