-
Notifications
You must be signed in to change notification settings - Fork 334
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 #2215 from zglx/master
- Loading branch information
Showing
6 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
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
94 changes: 94 additions & 0 deletions
94
...80902231/app/src/main/java/com/example/administrator/soft1614080902231/ThreeActivity.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,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
33
soft1614080902231/app/src/main/res/layout/activity_three.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,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> |
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,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"}] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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返回主线程后就可以了 |