-
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 #2287 from xiaojiahao/master
- Loading branch information
Showing
9 changed files
with
172 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[{ | ||
"word": "agreement", | ||
"mean": "n.协定;协议;契约;达成协议;同意,一致" | ||
}, | ||
{ | ||
"word": "adopt", | ||
"mean": "vt.采纳,采用;正式通过;领养;vi.采取;过继" | ||
} | ||
] |
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,25 @@ | ||
# 第六次实验 | ||
|
||
## 1.实验目标 | ||
掌握Android网络访问方法 | ||
## 2.实验内容 | ||
在个人目录中创建一个表示数据的XML或JSON文件<br> | ||
数据文件代码提交之后从GitHub获取文件URL<br> | ||
在应用中通过网络编程访问GitHub的数据文件<br> | ||
在应用中解析并显示文件所包含的数据;<br> | ||
将应用运行结果截图。<br> | ||
## 3.实验步骤 | ||
(1)、利用www.bejson.com校验json格式,并复制创建json文件上传至自己的GitHub。记下其URL | ||
(2)、新建activity,在app里的其中一个activity新建监听按钮,实现点击它跳转到该新的activity。 | ||
(3)、在AndroidManifest.xml中赋予其有关Internet的权限。 | ||
(4)、创建线程,创建URL对象读取url,使用url的方法获取数据。 | ||
(5)、利用输入流将url获取的数据以字符串读入 | ||
(6)、解析json,利用key获取对应的value,然后将其输出在listview上。 | ||
(5)、返回主线程 | ||
## 4.实验结果 | ||
|
||
![image](https://github.com/xiaojiahao/android-labs-2018/blob/master/soft1614080902319/sy6.png) | ||
|
||
## 5.实验体会 | ||
这次实验让我了解了一些线程和网络json,url有关的知识,AndroidManifest.xml是放权限的,这次我在一个activity放一张几百k的壁纸,卡爆, | ||
以后我会注意点,不能乱塞。 |
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
95 changes: 95 additions & 0 deletions
95
soft1614080902319/实验2/java/edu/hzuapps/androidlabs/Soft1614080902319Activity/Activity_4.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,95 @@ | ||
package edu.hzuapps.androidlabs.Soft1614080902319Activity; | ||
|
||
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 Activity_4 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_4); | ||
|
||
|
||
listView=(ListView)findViewById(R.id.danciben); | ||
getJson(); | ||
} | ||
public void getJson() { | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
try { | ||
//你的URL | ||
String url_s = "https://raw.githubusercontent.com/xiaojiahao/android-labs-2018/master/soft1614080902319/information.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>( | ||
Activity_4.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 word = person.getString("word"); | ||
String mean = person.getString("mean"); | ||
|
||
String msg; | ||
msg= word; | ||
Log.v("结果",msg); | ||
list.add(msg); | ||
msg= mean; | ||
Log.v("结果",msg); | ||
list.add(msg); | ||
|
||
} | ||
} catch (Exception e) { | ||
Log.v("出错","Worring"); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
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,18 @@ | ||
<?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=".Activity_4"> | ||
|
||
|
||
<ListView | ||
android:id="@+id/danciben" | ||
android:layout_width="457dp" | ||
android:layout_height="wrap_content" | ||
/> | ||
|
||
|
||
|
||
</android.support.constraint.ConstraintLayout> |
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,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<color name="colorPrimary">#3F51B5</color> | ||
<color name="colorPrimaryDark">#303F9F</color> | ||
<color name="colorAccent">#FF4081</color> | ||
</resources> |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<resources> | ||
<string name="app_name">词汇测试</string> | ||
<string name="entrance">开始测试</string> | ||
<string name="title_activity_3">Activity_3</string> | ||
<string name="danciben">单词本</string> | ||
</resources> |
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