Skip to content

Commit

Permalink
Merge pull request #2254 from ChenchenJT/master
Browse files Browse the repository at this point in the history
#6 #994 QQ the 6th lab
  • Loading branch information
zengsn authored May 26, 2018
2 parents c6c37f9 + 333e5bb commit 067b8d1
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 5 deletions.
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;
}
}
16 changes: 11 additions & 5 deletions soft1614080902335/manifests/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<activity android:name=".Soft1614080902335Activity4">
<activity android:name=".Soft1614080902335Activity6">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<activity android:name=".Soft1614080902335Activity2" />
<activity android:name=".Soft1614080902335Activity1" />
<activity android:name=".Soft1614080902335Activity3" />
<activity android:name=".Soft1614080902335Activity4"></activity>

<provider
android:name=".UserInformationProvider"
android:authorities="androidlabs.hzuapps.edu.user" />

<activity android:name=".Soft1614080902335Activity1" />
<activity android:name=".Soft1614080902335Activity3"></activity>
</application>

<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" />
</manifest>
43 changes: 43 additions & 0 deletions soft1614080902335/report6.md
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 soft1614080902335/res/layout/activity_soft1614080902335_qq6.xml
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>
6 changes: 6 additions & 0 deletions soft1614080902335/user_information.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "764774222",
"name": "陈嘉涛",
"sex": "",
"area": "广东惠州"
}
Binary file added soft1614080902335/实验六截图1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added soft1614080902335/实验六截图2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 067b8d1

Please sign in to comment.