diff --git a/soft1614080902335/java/androidlabs/hzuapps/edu/soft1614080902335/Soft1614080902335Activity6.java b/soft1614080902335/java/androidlabs/hzuapps/edu/soft1614080902335/Soft1614080902335Activity6.java new file mode 100644 index 000000000..0fd2e3fc1 --- /dev/null +++ b/soft1614080902335/java/androidlabs/hzuapps/edu/soft1614080902335/Soft1614080902335Activity6.java @@ -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; + } +} diff --git a/soft1614080902335/manifests/AndroidManifest.xml b/soft1614080902335/manifests/AndroidManifest.xml index 662642fd6..6ed866e54 100644 --- a/soft1614080902335/manifests/AndroidManifest.xml +++ b/soft1614080902335/manifests/AndroidManifest.xml @@ -9,21 +9,27 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.NoActionBar"> - + - + + + + - - - + + + + + + \ No newline at end of file diff --git a/soft1614080902335/report6.md b/soft1614080902335/report6.md new file mode 100644 index 000000000..f76fd7821 --- /dev/null +++ b/soft1614080902335/report6.md @@ -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。 diff --git a/soft1614080902335/res/layout/activity_soft1614080902335_qq6.xml b/soft1614080902335/res/layout/activity_soft1614080902335_qq6.xml new file mode 100644 index 000000000..286336050 --- /dev/null +++ b/soft1614080902335/res/layout/activity_soft1614080902335_qq6.xml @@ -0,0 +1,157 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +