Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5 #278 第五第六次实验报告 #2625

Merged
merged 15 commits into from
Jun 2, 2018
28 changes: 28 additions & 0 deletions soft1614080902108/ app/ src/ test/ res/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidlabs.soft1614080902108">

<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=".Soft1614080902110Activity3">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Soft1614080902110Activity2"></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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package androidlabs.hzuapps.edu.soft1614080902108;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
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;

public class Soft1614080902110Activity11 extends AppCompatActivity {
private EditText mEditTextContent;
private Button mButtonDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft161408090211011);
mEditTextContent = (EditText) findViewById(R.id.et_password);
Button button1 = (Button) findViewById(R.id.button_2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//向内部存储写入数据
saveContent();
Toast.makeText(Soft1614080902110Activity11.this, "已保存", Toast.LENGTH_SHORT).show();

}
});

mButtonDisplay = (Button) findViewById(R.id.button_3);
mButtonDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//从内部存储读取数据
String s = getContent();
Toast.makeText(Soft1614080902110Activity11.this, s, Toast.LENGTH_SHORT).show();

}
});

}

private String getContent() {
String s = null;
FileInputStream fis = null;
try {
//通过该方法从内部存储中的edit_data.txt文件中读取数据
fis = this.openFileInput("edit_data.txt");
int len = 0;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
s = new String(buf, 0, len, "UTF-8");

}


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return s;
}
private void saveContent() {
FileOutputStream fos = null;
String content = mEditTextContent.getText().toString();

try {


//通过该方法向内部存储中写入数据,文件名为edit_data.txt,模式为MODE_PRIVATE,表示该文件操作权限为文本应用,另一个常用的权限MODE_APPEND表示在文件末尾添加内容
fos = this.openFileOutput("edit_data.txt", MODE_PRIVATE);
fos.write(content.getBytes());
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();

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

}
}

}

}
117 changes: 117 additions & 0 deletions soft1614080902108/ app/ src/ test/ res/Soft1614080902108Activity2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package edu.hzuapps.androidlabs.soft1614080902108;



import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
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;

public class Soft1614080902110Activity2 extends AppCompatActivity {
private EditText mEditTextContent;
private Button mButtonDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft16140809021102);
mEditTextContent = (EditText) findViewById(R.id.et_password);
Button button1 = (Button) findViewById(R.id.button_2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//向内部存储写入数据
saveContent();
Toast.makeText(Soft1614080902110Activity2.this, "已保存", Toast.LENGTH_SHORT).show();

}
});

mButtonDisplay = (Button) findViewById(R.id.button_3);
mButtonDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//从内部存储读取数据
String s = getContent();
Toast.makeText(Soft1614080902110Activity2.this, s, Toast.LENGTH_SHORT).show();

}
});

}

private String getContent() {
String s = null;
FileInputStream fis = null;
try {
//通过该方法从内部存储中的edit_data.txt文件中读取数据
fis = this.openFileInput("edit_data.txt");
int len = 0;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
s = new String(buf, 0, len, "UTF-8");

}


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return s;
}
private void saveContent() {
FileOutputStream fos = null;
String content = mEditTextContent.getText().toString();

try {


//通过该方法向内部存储中写入数据,文件名为edit_data.txt,模式为MODE_PRIVATE,表示该文件操作权限为文本应用,另一个常用的权限MODE_APPEND表示在文件末尾添加内容
fos = this.openFileOutput("edit_data.txt", MODE_PRIVATE);
fos.write(content.getBytes());
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();

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

}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package edu.hzuapps.androidlabs.soft1614080902108;

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 Soft1614080902110Activity3 extends AppCompatActivity {
String text;
String Name;
String id;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft1614080902108);
getJson();
}
public void getJson() {
new Thread() {
@Override
public void run() {
try {
String url_user = "https://raw.githubusercontent.com/ccccssxxxx/android-labs-2018/master/soft1614080902108/MIKE.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();
System.out.println(text);
input.close();
inputStream.close();
Log.v("解析:", text);
parseJson();
}
} catch (Exception e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
((TextView) findViewById(R.id.text_view)).setText(Name);
((TextView) findViewById(R.id.text_view1)).setText(id);

}
});
}
}.start();
}

public void parseJson() {
JSONObject Json1 = null;
try {
Json1 = new JSONObject(text);
Name = Json1.getString("Name");
id = Json1.getString("id");
} catch (JSONException e) {
e.printStackTrace();
Log.v("Error", "Error!");
}

}

}
Loading