Skip to content

Commit

Permalink
Merge pull request #490 from QuanGuanQiao/master
Browse files Browse the repository at this point in the history
#6 #71 第六次实验
  • Loading branch information
zengsn authored Jun 19, 2017
2 parents 10576f6 + bc86de6 commit 376adf0
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903114;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.DownloadManager.Request;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;



public class AboutActivity extends Activity{
private TextView tv_softwareName, tv_authorName;
private String softwareName,authorName;
private Handler handler = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_net1414080903114);
handler = new Handler();
sendRequest();
}

private void sendRequest() {
new Thread(){
@Override
public void run() {
String url = "https://github.com/QuanGuanQiao/android-labs-2017/tree/master/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903114/about.json";
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
String responseData = response.body().string();
AnalysisJson(responseData);
} catch (IOException e) {
e.printStackTrace();
}
handler.post(runnableUi);
}
}.start();
}

private void AnalysisJson(String jsonData) {
try {
JSONArray jsonArray = new JSONArray(jsonData);
for (int i = 0; i < jsonArray.length(); i++ ) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
softwareName = jsonObject.getString("softwareName");
authorName = jsonObject.getString("authorName");
}
} catch (Exception e) {
e.printStackTrace();
}
}

Runnable runnableUi = new Runnable() {
public void run() {
tv_softwareName = (TextView) findViewById(R.id.tv_softwareName);
tv_softwareName.setText("softwareName: " + softwareName);
tv_authorName = (TextView) findViewById(R.id.tv_authorName);
tv_authorName.setText("authorName: " + authorName);

}
};

}

Original file line number Diff line number Diff line change
@@ -1,133 +1,143 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903114;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import edu.hzuapps.androidlabs.homeworks.net1414080903114.hardwareinfo.RateDatas;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private TextView tv_readRate, tv_writeRate, tv_uploadRate, tv_downloadRate;
private float virtualRate;


private Handler handler = new Handler( );
private Runnable gsRateThread = new Runnable(){
@Override
public void run() {
virtualRate++;
if (virtualRate == 1000) {
virtualRate = 0;
}
RateDatas rateD = new RateDatas();
rateD = getRateDatas();
setTextsValue(rateD);
handler.postDelayed(this,1000); //Causes the Runnable r to be added to the message queue,
//to be run after the specified amount of time elapses.
//The runnable will be run on the thread to which this handler is attached.
//2秒后运行this所指的进程。???
//因为在本进程内,所以相当于一个函数递归调用。 ??

}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_net1414080903114);
virtualRate = 0;
handler.postDelayed(gsRateThread,1000); // 开始Timer
}
public float getDiskReadR() {
float readR = 0;
//TODO code here to get diskReadRate
readR = virtualRate;
return readR;

}
public float getDiskWriteR() {
float writeR = 0;
//TODO code here to get diskWriteRate
writeR = virtualRate + 1;
return writeR;

}
public float getNetUploadR(){
float uploadR = 0;
//TODO code here to get networkUploadRate
uploadR = virtualRate + 2;
return uploadR;

}
public float getNetDownloadR(){
float downloadR = 0;
//TODO code here to get networkDownloadRate
downloadR = virtualRate + 3;
return downloadR;

}
public RateDatas getRateDatas() {
float readRate = 0, writeRate = 0, uploadRate = 0, downloadRate = 0;
RateDatas rateD = new RateDatas();
readRate = getDiskReadR();
writeRate = getDiskWriteR();
uploadRate = getNetUploadR();
downloadRate = getNetDownloadR();

rateD.setReadR(readRate);
rateD.setWriteR(writeRate);
rateD.setUploadR(uploadRate);
rateD.setDownloadR(downloadRate);
return rateD;
}
public void setTextsValue(RateDatas rateD) {
tv_readRate = (TextView) findViewById(R.id.tv_diskReadRate);
tv_writeRate = (TextView) findViewById(R.id.tv_diskWriteRate);
tv_readRate.setText("ReadRate: " + rateD.getReadR() + " MB/s");
tv_writeRate.setText("WriteRate: " + rateD.getWriteR() + " MB/s");

tv_uploadRate = (TextView) findViewById(R.id.tv_networkReadRate);
tv_downloadRate = (TextView) findViewById(R.id.tv_networkWriteRate);
tv_uploadRate.setText("UploadRate: " + rateD.getUploadR() + " KB/s");
tv_downloadRate.setText("DownloadRate: " + rateD.getDownloadR() + " KB/s");
}
public void saveRate(View view) {
FileOutputStream fos;
String rate = null;
try {
fos = openFileOutput("rateDatas.txt", MODE_APPEND);
RateDatas rateD = new RateDatas();
rateD = getRateDatas();
rate = "ReadRate = " + rateD.getReadR() + "\n" +
"WriteRate = " + rateD.getWriteR() + "\n" +
"UploadRate = " + rateD.getUploadR() + "\n" +
"DownloadRate = " + rateD.getDownloadR() + "\n";
fos.write(rate.getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "save successfully", 0).show();
}
public void readRate(View view) throws IOException {
FileInputStream fis = openFileInput("rateDatas.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
String rateDatas = new String(buffer);
fis.close();
Toast.makeText(MainActivity.this, rateDatas, 0).show();
}

}

package edu.hzuapps.androidlabs.homeworks.net1414080903114;

import java.io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import edu.hzuapps.androidlabs.homeworks.net1414080903114.hardwareinfo.RateDatas;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity {
private TextView tv_readRate, tv_writeRate, tv_uploadRate, tv_downloadRate;
private float virtualRate;


private Handler handler = new Handler( );
private Runnable gsRateThread = new Runnable(){
@Override
public void run() {
virtualRate++;
if (virtualRate == 1000) {
virtualRate = 0;
}
RateDatas rateD = new RateDatas();
rateD = getRateDatas();
setTextsValue(rateD);
handler.postDelayed(this,1000); //Causes the Runnable r to be added to the message queue,
//to be run after the specified amount of time elapses.
//The runnable will be run on the thread to which this handler is attached.
//2秒后运行this所指的进程。???
//因为在本进程内,所以相当于一个函数递归调用。 ??

}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_net1414080903114);
virtualRate = 0;
handler.postDelayed(gsRateThread,1000); // 开始Timer
}
public float getDiskReadR() {
float readR = 0;
//TODO code here to get diskReadRate
readR = virtualRate;
return readR;

}
public float getDiskWriteR() {
float writeR = 0;
//TODO code here to get diskWriteRate
writeR = virtualRate + 1;
return writeR;

}
public float getNetUploadR(){
float uploadR = 0;
//TODO code here to get networkUploadRate
uploadR = virtualRate + 2;
return uploadR;

}
public float getNetDownloadR(){
float downloadR = 0;
//TODO code here to get networkDownloadRate
downloadR = virtualRate + 3;
return downloadR;

}
public RateDatas getRateDatas() {
float readRate = 0, writeRate = 0, uploadRate = 0, downloadRate = 0;
RateDatas rateD = new RateDatas();
readRate = getDiskReadR();
writeRate = getDiskWriteR();
uploadRate = getNetUploadR();
downloadRate = getNetDownloadR();

rateD.setReadR(readRate);
rateD.setWriteR(writeRate);
rateD.setUploadR(uploadRate);
rateD.setDownloadR(downloadRate);
return rateD;
}
public void setTextsValue(RateDatas rateD) {
tv_readRate = (TextView) findViewById(R.id.tv_diskReadRate);
tv_writeRate = (TextView) findViewById(R.id.tv_diskWriteRate);
tv_readRate.setText("ReadRate: " + rateD.getReadR() + " MB/s");
tv_writeRate.setText("WriteRate: " + rateD.getWriteR() + " MB/s");

tv_uploadRate = (TextView) findViewById(R.id.tv_networkReadRate);
tv_downloadRate = (TextView) findViewById(R.id.tv_networkWriteRate);
tv_uploadRate.setText("UploadRate: " + rateD.getUploadR() + " KB/s");
tv_downloadRate.setText("DownloadRate: " + rateD.getDownloadR() + " KB/s");
}
public void saveRate(View view) {
FileOutputStream fos;
String rate = null;
try {
fos = openFileOutput("rateDatas.txt", MODE_APPEND);
RateDatas rateD = new RateDatas();
rateD = getRateDatas();
rate = "ReadRate = " + rateD.getReadR() + "\n" +
"WriteRate = " + rateD.getWriteR() + "\n" +
"UploadRate = " + rateD.getUploadR() + "\n" +
"DownloadRate = " + rateD.getDownloadR() + "\n";
fos.write(rate.getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "save successfully", 0).show();
}
public void readRate(View view) throws IOException {
FileInputStream fis = openFileInput("rateDatas.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
String rateDatas = new String(buffer);
fis.close();
Toast.makeText(MainActivity.this, rateDatas, 0).show();
}

public void aboutSoftware() {
Intent intent=new Intent(this, AboutActivity.class);
startActivity(intent);

}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"softwareName":"AndroidLabs","authorName":"QuanGuanQiao"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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"
android:orientation="vertical" >


<TextView
android:id="@+id/tv_softwareName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/tv_authorName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</LinearLayout>
Loading

0 comments on commit 376adf0

Please sign in to comment.