Skip to content

Commit

Permalink
#56,实验5和实验6
Browse files Browse the repository at this point in the history
  • Loading branch information
chinesehope committed Jun 2, 2018
1 parent 5bef6a1 commit 4f10053
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 1 deletion.
99 changes: 99 additions & 0 deletions soft1614080902345/App/Java/Json.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.example.administrator;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
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 Json extends AppCompatActivity {
private String text;
private ListView listView;
private ArrayAdapter<String> adapter;
private List<String> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.json);
listView=(ListView)findViewById(R.id.listview);
getjson();
}
public void getjson()
{
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("https://raw.githubusercontent.com/chinesehope/android-labs-2018/master/soft1614080902345/message1.json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
connection.setUseCaches(false);
connection.connect();
InputStream in = connection.getInputStream();
InputStreamReader input=new InputStreamReader(in);
BufferedReader reader=new BufferedReader(input);
if (connection.getResponseCode() == 200) {
StringBuilder response = new StringBuilder();
String Line;
while ((Line = reader.readLine()) != null) {
response.append(Line);
}
text = response.toString();
Log.v("out---------------->",text);
analyzejson();
}
}
catch(Exception e)
{
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter=new ArrayAdapter<String>(Json.this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
});
}
}).start();
}
public void analyzejson()
{
try{
JSONArray jsonArray=new JSONArray(text);
for(int a=0;a<jsonArray.length();a++)
{
JSONObject jsonObject=jsonArray.getJSONObject(a);
String number=jsonObject.getString("地点");
String number1=jsonObject.getString("时间");
String number2=jsonObject.getString("内容");
String add;
add=number+" "+number1+" "+number2;
list.add(add);
Log.v("结果",add);
}

}
catch (Exception e)
{
e.printStackTrace();
}
}

}
10 changes: 9 additions & 1 deletion soft1614080902345/App/Java/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
private Button runbutton,run2button;
private Button runbutton,run2button,jsonbutton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runbutton=(Button) findViewById(R.id.button1);
run2button=(Button) findViewById(R.id.button2);
jsonbutton=(Button) findViewById(R.id.button);
runbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -29,6 +30,13 @@ public void onClick(View v) {
startActivity(intent);
}
});
jsonbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent(MainActivity.this,Json.class);
startActivity(intent);
}
});

}
}
6 changes: 6 additions & 0 deletions soft1614080902345/App/Main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
android:label="兼职页面">

</activity>
<activity
android:name=".Json"
android:screenOrientation="portrait"
android:label="兼职页面">

</activity>

</application>

Expand Down
10 changes: 10 additions & 0 deletions soft1614080902345/App/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@

tools:layout_editor_absoluteX="169dp"
tools:layout_editor_absoluteY="272dp" />

<Button
android:id="@+id/button"
android:layout_marginTop="100dp"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="解 码" />

<Button
android:layout_marginTop="100dp"
android:layout_marginLeft="30dp"
Expand Down
19 changes: 19 additions & 0 deletions soft1614080902345/App/res/layout/json.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"

/>
</LinearLayout>
</LinearLayout>
33 changes: 33 additions & 0 deletions soft1614080902345/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 第六次实验

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

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

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

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

5.将应用运行结果截图。

## 2、实验步骤
1.通过网上的json在线编辑器,然后编写一个json文件,编写完下载到本地。

2.然后把json文件提交到自己github上的库中

3.然后在Android studio 上创建一个新的Activity,从github获取json文件URL,再通过BufferedReader对服务器的流进行读取,再调用函数进行json解析。

4.在AndroidManifest.xml添加对应代码获取网络权限



## 3、实验结果
我的Android应用第一个activity截图。
![在Android SDK Manager中选择6.0库](https://raw.githubusercontent.com/chinesehope/android-labs-2018/master/soft1614080902345/%E5%AE%9E%E9%AA%8C6%E6%88%AA%E5%9B%BE2.jpg "配置教育网下载代理")

点击按钮后跳转的activity.
![在Android SDK Manager中选择6.0库](https://raw.githubusercontent.com/chinesehope/android-labs-2018/master/soft1614080902345/%E5%AE%9E%E9%AA%8C6%E6%88%AA%E5%9B%BE1.jpg "配置教育网下载代理")

## 4、实验体会
1. 这个实验我有了对网络权限的理解,和对json解析的理解,通过这次实验,我学会了json文件的解析,以及Liteview的使用。

0 comments on commit 4f10053

Please sign in to comment.