Skip to content

Commit

Permalink
Merge pull request #2274 from HHuangF/master
Browse files Browse the repository at this point in the history
#6 #991 第六次实验
  • Loading branch information
zengsn authored May 26, 2018
2 parents 7a581d8 + aec5f18 commit 06b6f82
Show file tree
Hide file tree
Showing 15 changed files with 505 additions and 6 deletions.
Binary file added soft1614080902440/Sixth/Soft1614080902440png1.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 soft1614080902440/Sixth/Soft1614080902440png2.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 soft1614080902440/Sixth/Soft1614080902440png3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions soft1614080902440/Sixth/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# 实验六实验报告

## 实验目的

### 1、Android网络编程

## 实验要求

### 1、掌握Android网络访问方法;

### 2、理解XML和JSON表示数据的方法。

## 实验内容一(选做一)

### 1、在个人目录中创建一个表示数据的XML或JSON文件;

### 2、数据文件代码提交之后从GitHub获取文件URL;

### 3、在应用中通过网络编程访问GitHub的数据文件;

### 4、在应用中解析并显示文件所包含的数据;

### 5、将应用运行结果截图。

## 实验内容二(选做二)

### 1、从网络下载一个文件(图片、MP3、MP4);

### 2、保存到手机,在应用中使用文件;

### 3、将应用运行结果截图。

## 实验步骤

### 1、阅读实验要求:https://github.com/hzuapps/android-labs-2018/labels/Lab;

### 2、在电脑上Android Studio编写代码

#### ①、运行Android studio;

#### ②、创建一个新的Activity:Soft1614080902440Activity3.java;

#### ③、在activity3_soft1614080902440.xml中添加一个TextView控件(用于查看存储获得的数据)、一个ListView控件(用于查看解析后的数据)、两个Button控件(用于获得数据和解析数据)和四个EditText控件(用于显示属性名);

#### ④、在Soft1614080902440Activity3.java中对两个Button添加监听器、获取json文件函数、解析json文件函数和获取并解析json文件函数(注意要在子线程中进行);

#### ⑤、在两个监听器中分别调用获取json文件函数和获取并解析json文件函数;

#### ⑥、在activity3_soft1614080902440.xml中添加多一个Button控件(用于跳转),在Soft1614080902440Activity2.java中对这个Button添加跳转到Soft1614080902440Activity3.java的监听器。

### 3、使用Git将代码提交到自己的库中:https://github.com/HHuangF/android-labs-2018;
$ git pull
$ git add soft1614080902440/*
$ git commit "#6 #991 第六次实验 "
$ git push
### 4、在自己的GitHub库上创建和发送Pull Request;
### 5、在GitHub中使用Markdown文件编写实验报告(report6.md);

## 实验结果

![运行截图](https://github.com/HHuangF/android-labs-2018/blob/master/soft1614080902440/Sixth/Soft1614080902440png1.png)

### 获取json文件数据

![运行截图](https://github.com/HHuangF/android-labs-2018/blob/master/soft1614080902440/Sixth/Soft1614080902440png2.png)

### 解析json文件数据

![运行截图](https://github.com/HHuangF/android-labs-2018/blob/master/soft1614080902440/Sixth/Soft1614080902440png3.png)


## 实验体会

### 这次实验主要的难点在与解析json文件并显示出来和UI的更新,刚开始很多次都是获取到了json文件,却无法解析并显示文件;通过这次是让我了解到了网络编程解析json文件的四种方法(JSONObject、Gson、JackSon、Fastjson)和网络编程的功能。
13 changes: 10 additions & 3 deletions soft1614080902440/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.androidlabs.soft1614080902440">

<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" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -17,7 +22,9 @@
</intent-filter>
</activity>
<activity android:name=".Soft1614080902440Activity1" />
<activity android:name=".Soft1614080902440Activity2"></activity>
<activity android:name=".Soft1614080902440Activity2" />
<activity android:name=".Soft1614080902440Activity3"></activity>
</application>

</manifest>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package edu.androidlabs.soft1614080902440;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Soft1614080902440Activity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft1614080902440);
final Activity thisActivity = this;

Button btnOpen = (Button) findViewById(R.id.button_open);
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(thisActivity,Soft1614080902440Activity1.class);
thisActivity.startActivity(intent);

}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package edu.androidlabs.soft1614080902440;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Soft1614080902440Activity1 extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1_soft1614080902440);
final EditText mUserName=(EditText)this.findViewById(R.id.login_username);
final EditText mPassWord=(EditText)this.findViewById(R.id.login_password);
Button mSubmit=(Button)this.findViewById(R.id.login_submit);
final TextView mResult=(TextView) this.findViewById(R.id.login_result);
mSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName=mUserName.getText().toString();
String password=mPassWord.getText().toString();
//默认登陆账号为:admin;密码为:123456
if("admin".equals(userName)&&"123456".equals(password)){

Intent intent = new Intent(Soft1614080902440Activity1.this,Soft1614080902440Activity2.class);
Soft1614080902440Activity1.this.startActivity(intent);


}else{
mResult.setText("登陆失败,请重新输入!");
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Expand All @@ -21,6 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {
//获取UI对象
Button bt_write = (Button) findViewById(R.id.write);
Button bt_read = (Button) findViewById(R.id.read);
Button bt_next = (Button) findViewById(R.id.next);
//对写入数据的按钮添加监听器
bt_write.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -38,6 +40,15 @@ public void onClick(View view) {

}
});
//对跳转的按钮添加监听器
bt_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Soft1614080902440Activity2.this,Soft1614080902440Activity3.class);
Soft1614080902440Activity2.this.startActivity(intent);

}
});
}
//写入数据
public void write(String text) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package edu.androidlabs.soft1614080902440;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

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 Soft1614080902440Activity3 extends Activity {
private String text;
private String name;
private String sex;
private int age;
private String subject;
private String msg;
private ListView listView;
private ArrayAdapter<String> adapter1;
private List<String> list = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity3_soft1614080902440);
//获得UI对象
Button bt_get = (Button) findViewById(R.id.get);
Button bt_parse = (Button) findViewById(R.id.parse);
listView=(ListView)findViewById(R.id.ListView_1);
//对获取json文件数据的按钮添加监听器
bt_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getJson();

}
});
//对获取并解析json文件数据的按钮添加监听器
bt_parse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getParseJson();
}
});

}
//获得json文件数据
public void getJson(){
new Thread() {
@Override
public void run() {
try {
//你的URL
String url_s = "https://raw.githubusercontent.com/HHuangF/android-labs-2018/master/soft1614080902440/data.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) {//200意味着返回的是"OK"
String inputLine;
StringBuffer resultData = new StringBuffer();//StringBuffer字符串拼接很快
while ((inputLine = buffer.readLine()) != null) {
resultData.append(inputLine);
}
text = resultData.toString();
Log.v("out---------------->", text);
}
} catch (Exception e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView) findViewById(R.id.get_result)).setText(text);

}
});
}
}.start();
}
//解析json文件数据
public void parseJson(){
try{
//这里的text就是上边获取到的数据,一个String.
JSONArray jsonArray = new JSONArray(text);
for(int i =0;i<jsonArray.length();i++){
JSONObject person = jsonArray.getJSONObject(i);
name = person.getString("name");
sex = person.getString("sex");
age = person.getInt("age");
subject = person.getString("subject");
msg = name+" "+sex+" "+age+" "+subject;
Log.v("result",msg);
list.add(msg);
}
} catch(Exception e){
e.printStackTrace();
}

}
//获得并解析json文件数据
public void getParseJson(){
new Thread() {
@Override
public void run() {
try {
//你的URL
String url_s = "https://raw.githubusercontent.com/HHuangF/android-labs-2018/master/soft1614080902440/data.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) {//200意味着返回的是"OK"
String inputLine;
StringBuffer resultData = new StringBuffer();//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>(
Soft1614080902440Activity3.this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter1);
}
});
}
}.start();
}

}
Loading

0 comments on commit 06b6f82

Please sign in to comment.