Skip to content

Commit

Permalink
Merge pull request #2615 from 1614080902239/master
Browse files Browse the repository at this point in the history
#5 #892 作业查询系统 #2604
  • Loading branch information
zengsn authored Jun 2, 2018
2 parents d5da6d3 + 91abb8b commit 439b20a
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 0 deletions.
Binary file added soft1614080902239/555.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 soft1614080902239/666.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 soft1614080902239/777.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package edu.hzuapps.androidlabs.soft1614080902239;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Formatter;
import java.util.Locale;

public class Soft1614080902239Activity extends AppCompatActivity {

private Button save, read, delete;
private EditText content;
private TextView show;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft1614080902239);
save = (Button) findViewById(R.id.save);
read = (Button) findViewById(R.id.read);
delete = (Button) findViewById(R.id.delete);
content = (EditText) findViewById(R.id.content);
show = (TextView) findViewById(R.id.show);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 调用SAveFile方法 文件保存到date/date目录下
saveFile();
}
});
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show.setText(readFile());
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeFile();
}
});
}

//删除文件
public void removeFile() {
String[] files = fileList();
for (String str : files) {
if (str.equals("text.text")) {
deleteFile(str);
break;
}
}
}

//从内存储中读取文件
public String readFile() {
// BufferedReader包装流(字符流 字节流) 带缓冲区的
BufferedReader reader = null;
FileInputStream fis = null;
StringBuilder sbd = new StringBuilder();
try {
fis = openFileInput("text.text");
reader = new BufferedReader(new InputStreamReader(fis));
sbd.append(getFilesDir().getCanonicalPath());

String row = "";
while ((row = reader.readLine()) != null) {
sbd.append(row);
}
} catch (FileNotFoundException e) {
Toast.makeText(getBaseContext(), "文件不存在", Toast.LENGTH_SHORT).show();
//e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sbd.toString();
}

//保存文件到内存储
public void saveFile() {
FileOutputStream fos = null;
try {
/*
openFileOutput返回一个输出字节流
指向的路径为data/data/包名/files
参数1:文件名称(如果不存在则自动创建)
参数2:模式MODE_APPEND文件内容可樶加
模式MODE_PRIVATE文件内容被覆盖
*/
fos = openFileOutput("text.text", MODE_APPEND);
String str = content.getText().toString();
fos.write(str.getBytes());
Toast.makeText(getBaseContext(), "保存成功", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Soft1614080902239Activity">
<EditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />

<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="116dp"
android:layout_marginTop="84dp"
android:text="保存作业文件"
android:layout_alignParentLeft="true"
android:layout_marginLeft="116dp" />

<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/save"
android:layout_marginTop="204dp"
android:text="查看作业文件"
android:layout_alignLeft="@+id/save" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/show"
android:layout_below="@id/save"/>

<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="160dp"
android:layout_marginStart="127dp"
android:text="删除文件"
android:layout_alignParentLeft="true"
android:layout_marginLeft="127dp" />
</RelativeLayout>
26 changes: 26 additions & 0 deletions soft1614080902239/app3/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidlabs.soft1614080902239">
<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"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

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

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.json.JSONArray;
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;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private List<String> list=new ArrayList<>();
private String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= findViewById(R.id.view);
getJson();
}

//读取Test.json文件
public void getJson(){
new Thread(new Runnable() {
@Override
public void run() {
try {
//文件的URL
String url_s="https://raw.githubusercontent.com/1614080902239/android-labs-2018/master/soft1614080902239/content.json";
URL url = new URL(url_s);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置连接属性
conn.setConnectTimeout(5000);//设置超时
//conn.setRequestMethod("GET");
//conn.setDoInput(true);
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;//用来保存每行读取的内容
StringBuilder resultData = new StringBuilder();//StringBuffer字符串拼接很快
while((inputLine = buffer.readLine())!= null){ //如果inputline为空说明读完了
resultData.append(inputLine); //将读到的内容添加到resultData中
}
text = resultData.toString();
Log.v("out---------------->",text);
parseJson();
}
} catch(Exception e){
e.printStackTrace();
}
//返回主线程
runOnUiThread(new Runnable() {
@Override
public void run() {
//创建一个数组适配器
//第一个参数是上下文,即当前的Activity,第二个参数是android sdk中自己内置的一个布局,表明数组每一条数据的布局,第三个参数就是我们要显示的数据
adapter= new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);//通过adapter把数据映射到ListView中
}
});
}
}).start();
}

//解析Text.json文件
public void parseJson(){
try {
JSONArray jsonArray=new JSONArray(text); //把字符串转成JSONArray对象
for(int i=0;i<jsonArray.length();i++){
JSONObject thing=jsonArray.getJSONObject(i); //遍历jsonArray数组,把每一个对象转成json对象
String content=thing.getString("content");
String subject=thing.getString("subject");
String time=thing.getString("time");
String msg;
msg=content+" "+subject+" "+time+" ";
Log.v("结果",msg);
list.add(msg);
}
} catch (JSONException e) {
Log.v("出错","Wrong");
e.printStackTrace();
}
}
}
20 changes: 20 additions & 0 deletions soft1614080902239/app3/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="作业查询:"
android:textSize="30sp"
tools:ignore="HardcodedText" />
<ListView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="461dp"
android:layout_alignParentBottom="true"
android:textSize="50sp"
/>
</RelativeLayout>
7 changes: 7 additions & 0 deletions soft1614080902239/content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"content":"实验十一",
"subject":"汇编语言",
"time":"周一"
}
]
33 changes: 33 additions & 0 deletions soft1614080902239/report5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 第五次实验

## 一、实验目的

1.掌握在Android App中存储数据

## 二、实验内容

1、打开已建好的项目soft1614080902239

2、将添加行程时产生的数据保存到文件存储中;

3、文件存储方式:内部;

4、实现界面控件的事件处理,例如点击按钮

5、运行程序并截图

6、使用git将代码提交到自己的库里

7、在自己的GitHub库上创建和发送Pull Request

8、在GitHub中编写实验报告

## 三、实验结果

!555.png (277×561) https://raw.githubusercontent.com/1614080902239/android-labs-2018/1dd27cdf39ef13974f6c9f52c13c8438064c9b7d/soft1614080902239/555.png
!666.png (284×589) https://raw.githubusercontent.com/1614080902239/android-labs-2018/1dd27cdf39ef13974f6c9f52c13c8438064c9b7d/soft1614080902239/666.png

## 四、实验体会

文件存储的方式有很多种,在这次实验中我采取了内部存储的存储方式,操作起来相对还是比较简单的,虽然有些地方刚开始有点乱,所幸后面经过室友跟老师的指导之后弄懂了,很感谢!之后会更加努力摸索出安卓中更多的奥妙。

Loading

0 comments on commit 439b20a

Please sign in to comment.