Skip to content

Commit

Permalink
Merge pull request #2287 from xiaojiahao/master
Browse files Browse the repository at this point in the history
#6 #1161 第六次实验
  • Loading branch information
zengsn authored May 26, 2018
2 parents 1db0f44 + 469df04 commit 5774909
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 2 deletions.
9 changes: 9 additions & 0 deletions soft1614080902319/information.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[{
"word": "agreement",
"mean": "n.协定;协议;契约;达成协议;同意,一致"
},
{
"word": "adopt",
"mean": "vt.采纳,采用;正式通过;领养;vi.采取;过继"
}
]
25 changes: 25 additions & 0 deletions soft1614080902319/report6.md
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的壁纸,卡爆,
以后我会注意点,不能乱塞。
Binary file added soft1614080902319/sy6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions soft1614080902319/实验2/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidlabs.Soft1614080902319Activity">

<uses-permission android:name="android.permission.INTERNET"/>
<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 @@ -16,7 +20,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity_2"></activity>
<activity android:name=".Activity_2" />
<activity android:name=".Activity_3" />
<activity android:name=".Activity_4"></activity>
</application>

</manifest>
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();
}
}

}
18 changes: 18 additions & 0 deletions soft1614080902319/实验2/res/layout/activity_4.xml
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>
6 changes: 6 additions & 0 deletions soft1614080902319/实验2/res/values/colors.xml
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>
2 changes: 2 additions & 0 deletions soft1614080902319/实验2/res/values/strings.xml
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>
9 changes: 9 additions & 0 deletions soft1614080902319/实验2/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

0 comments on commit 5774909

Please sign in to comment.