-
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 #2254 from ChenchenJT/master
- Loading branch information
Showing
7 changed files
with
314 additions
and
5 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
...4080902335/java/androidlabs/hzuapps/edu/soft1614080902335/Soft1614080902335Activity6.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,97 @@ | ||
package androidlabs.hzuapps.edu.soft1614080902335; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.widget.TextView; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public class Soft1614080902335Activity6 extends AppCompatActivity { | ||
|
||
String text; | ||
String user_id; | ||
String user_name; | ||
String user_sex; | ||
String user_area; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_soft1614080902335_qq6); | ||
getJson(); | ||
} | ||
|
||
public void getJson() { | ||
new Thread() { | ||
@Override | ||
public void run() { | ||
try { | ||
//你的URL | ||
String url_user = "https://raw.githubusercontent.com/ChenchenJT/android-labs-2018/master/soft1614080902335/user_information.json"; | ||
URL url = new URL(url_user); | ||
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();input.close(); | ||
inputStream.close(); | ||
Log.v("Json解析:", text); | ||
backJson(); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
((TextView) findViewById(R.id.new_id)).setText(user_id); | ||
((TextView) findViewById(R.id.new_name)).setText(user_name); | ||
((TextView) findViewById(R.id.new_sex)).setText(user_sex); | ||
((TextView) findViewById(R.id.new_area)).setText(user_area); | ||
} | ||
}); | ||
} | ||
}.start(); | ||
} | ||
|
||
|
||
public void backJson() { | ||
JSONObject person = null; | ||
try { | ||
person = new JSONObject(JSONTokener(text)); | ||
user_id=person.getString("id"); | ||
user_name=person.getString("name"); | ||
user_sex=person.getString("sex"); | ||
user_area=person.getString("area"); | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
Log.v("错误","Wrong!"); | ||
} | ||
|
||
} | ||
public String JSONTokener(String in) { | ||
// consume an optional byte order mark (BOM) if it exists | ||
if (in != null && in.startsWith("\ufeff")) { | ||
in = in.substring(1); | ||
} | ||
return in; | ||
} | ||
} |
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
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,43 @@ | ||
# 第六次实验 | ||
Android网络编程。 | ||
## 1. 实验目标 | ||
1. 掌握Android网络访问方法; | ||
2. 理解XML和JSON表示数据的方法。 | ||
## 2. 实验内容 | ||
|
||
1 在个人目录中创建一个表示数据的XML或JSON文件; | ||
2 数据文件代码提交之后从GitHub获取文件URL; | ||
3 在应用中通过网络编程访问GitHub的数据文件; | ||
4 在应用中解析并显示文件所包含的数据; | ||
5 将应用运行结果截图。 | ||
|
||
## 3. 实验步骤 | ||
|
||
1 打开Android Studio,新建一个Soft1614080902335Activity6,还有对应的layout.xml; | ||
2 通过HttpURLConnectionr类来建立对URL的连接; | ||
3 对于解析网络上的Json,必须先通过将其转换为字节数组,然后在进行操作,所以这里使用到了InputStream类; | ||
4 将得到的字节数组通过JSONObject类建立对象; | ||
5 通过对成员变量(key)获取对应的值(value); | ||
6 在通过findViewById获取控件Id,在用setText设置控件文本; | ||
7 代码实现后,通过git将app目录结构且有编码、有添加的文件先储存在自己clone下来的目录下,后上传到GitHub。 | ||
|
||
git指令代码如下: | ||
git add java/* | ||
git add layout/* | ||
git commit -m "#6 #994 QQ the 6th lab" | ||
git push | ||
|
||
8 继续上传report6.md文件,写实验报告 | ||
## 4. 实验结果 | ||
1 虚拟机运行结果: | ||
|
||
![Image text](https://github.com/ChenchenJT/android-labs-2018/blob/master/soft1614080902335/%E5%AE%9E%E9%AA%8C%E5%85%AD%E6%88%AA%E5%9B%BE1.png) | ||
|
||
|
||
2 通过Log.v输出字节数组: | ||
|
||
![Image text](https://github.com/ChenchenJT/android-labs-2018/blob/master/soft1614080902335/%E5%AE%9E%E9%AA%8C%E5%85%AD%E6%88%AA%E5%9B%BE2.png) | ||
|
||
## 5.实验体会 | ||
1 这次实验是解析网络上的Json,通过自己查找Json书写格式和获取方法写的,这次实验收获挺大的; | ||
2 这次实验一开始一直出错在Json格式,原来到最后是没有点击Raw。 |
157 changes: 157 additions & 0 deletions
157
soft1614080902335/res/layout/activity_soft1614080902335_qq6.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,157 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:background="@color/graywhite" | ||
android:orientation="vertical" | ||
tools:context=".Soft1614080902335Activity6"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" > | ||
<include layout="@layout/activity_soft1614080902335_qqhead"/> | ||
</LinearLayout> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="50dp" | ||
android:layout_marginTop="30dp"> | ||
<TextView | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:layout_marginTop="6dp" | ||
android:paddingLeft="35dp" | ||
android:text="QQ号" | ||
android:textColor="@color/black" | ||
android:textSize="25sp"/> | ||
<TextView | ||
android:id="@+id/new_id" | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:textAlignment="center" | ||
android:layout_marginTop="6dp" | ||
android:paddingLeft="20dp" | ||
android:text="764774222" | ||
android:textSize="25sp" | ||
android:textColor="@color/gray"/> | ||
</LinearLayout> | ||
|
||
<View android:layout_height="1px" | ||
android:layout_width="match_parent" | ||
android:layout_marginLeft="30dp" | ||
android:background="@color/gray" /> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="50dp"> | ||
<TextView | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:textAlignment="center" | ||
android:layout_marginTop="6dp" | ||
android:text="昵称" | ||
android:textColor="@color/black" | ||
android:textSize="25sp"/> | ||
<TextView | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:background="@color/graywhite"/> | ||
<TextView | ||
android:id="@+id/new_name" | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:textAlignment="center" | ||
android:layout_marginTop="6dp" | ||
android:textSize="25sp" | ||
android:textColor="@color/gray"/> | ||
</LinearLayout> | ||
|
||
<View android:layout_height="1px" | ||
android:layout_width="match_parent" | ||
android:layout_marginLeft="30dp" | ||
android:background="@color/gray" /> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="50dp"> | ||
<TextView | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:textAlignment="center" | ||
android:text="性别" | ||
android:layout_marginTop="6dp" | ||
android:textColor="@color/black" | ||
android:textSize="25sp"/> | ||
<TextView | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:background="@color/graywhite"/> | ||
<TextView | ||
android:id="@+id/new_sex" | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:textAlignment="center" | ||
android:layout_marginTop="6dp" | ||
android:textSize="25sp" | ||
android:textColor="@color/gray"/> | ||
</LinearLayout> | ||
|
||
<View android:layout_height="1px" | ||
android:layout_width="match_parent" | ||
android:layout_marginLeft="30dp" | ||
android:background="@color/gray" /> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="50dp"> | ||
<TextView | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:textAlignment="center" | ||
android:layout_marginTop="6dp" | ||
android:text="地区" | ||
android:textColor="@color/black" | ||
android:textSize="25sp"/> | ||
<TextView | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:background="@color/graywhite"/> | ||
<TextView | ||
android:id="@+id/new_area" | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
android:layout_weight="1" | ||
android:textAlignment="center" | ||
android:layout_marginTop="6dp" | ||
android:textSize="25sp" | ||
android:textColor="@color/gray"/> | ||
</LinearLayout> | ||
|
||
<View android:layout_height="1px" | ||
android:layout_width="match_parent" | ||
android:layout_marginLeft="30dp" | ||
android:background="@color/gray" /> | ||
|
||
<Button | ||
android:id="@+id/button_c" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="50dp" | ||
android:background="@color/backgroundcolor" | ||
android:text="修改资料" | ||
android:textSize="20sp" | ||
android:layout_marginLeft="20dp" | ||
android:layout_marginRight="20dp"/> | ||
|
||
</LinearLayout> |
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 @@ | ||
{ | ||
"id": "764774222", | ||
"name": "陈嘉涛", | ||
"sex": "男", | ||
"area": "广东惠州" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.