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

#6 #957 第六次实验 #2179

Merged
merged 7 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion soft1614080902221/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidlabs.soft1614080902221">

<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 @@ -18,7 +25,10 @@
</activity>
<activity android:name=".Activity2" />
<activity android:name=".Activity3" />
<activity android:name=".Activity4"></activity>
<activity android:name=".Activity4" />
<activity android:name=".Activity5"></activity>


</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ protected void onCreate(Bundle savedInstanceState) {
initButton(); //实现中间1-9个Button功能
timer = (TextView) findViewById(R.id.timer); //获取计时控件
handler.postDelayed(runnable, 1000); //实现右上角计时功能
rankList(); //实现排行榜Button功能

rankList(); //实现本地排行榜Button功能
networkListButton();
}


//显示当前玩家信息
private void initCurrentPlayerTextView() {
Intent recIntent = getIntent();//获取Activity2传递过来的玩家信息
Expand Down Expand Up @@ -134,6 +135,17 @@ public void onClick(View v) {

}

private void networkListButton() {
Button networkListButton = (Button)findViewById(R.id.NetworkListButton);
networkListButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent jumpIntent = new Intent(Activity3.this, Activity5.class);
startActivity(jumpIntent);
}
});
}

//强制玩家必须按1-9的数字顺序点击,否则不能消去
class ButtonMonitor implements View.OnClickListener {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package edu.hzuapps.androidlabs.soft1614080902221;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class Activity5 extends AppCompatActivity {

private List<PlayerData> playerDataList = new ArrayList<PlayerData>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_5);
String address = "https://raw.githubusercontent.com/yangyangyang2017/android-labs-2018/master/soft1614080902221/网络排行榜.json";
//发送请求
HttpUtil.sendOkHttpRequest(address, new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("排行榜","网络异常");
}

@Override
public void onResponse(Call call, Response response) throws IOException {
//获取的json数据
String responseData = response.body().string();
try {
//解析json
JSONArray jsonArray = new JSONArray(responseData);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String strPlayerName = jsonObject.getString("playerName");
String strSex = jsonObject.getString("sex");
String strGameCharacters = jsonObject.getString("gameCharacters");
int intTime = jsonObject.getInt("time");
PlayerData playerData = new PlayerData(strPlayerName, strSex, strGameCharacters, intTime);
playerDataList.add(playerData);

}


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

//更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
//通过id找到RecyclerView2组件对象
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view2);
//创建一个LinearLayoutManager对象
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Activity5.this);
//设置recyclerView的布局方式为LinearLayout
recyclerView.setLayoutManager(linearLayoutManager);
//创建适配器,并将playerDataList传入PlayerDataAdapter的构造函数中
PlayerDataAdapter playerDataAdapter = new PlayerDataAdapter(playerDataList);
//给recyclerView设置适配器
recyclerView.setAdapter(playerDataAdapter);
}
});


}
});


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

import okhttp3.OkHttpClient;
import okhttp3.Request;

public class HttpUtil{

public static void sendOkHttpRequest(String address,okhttp3.Callback callback){

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(address)
.build();
client.newCall(request).enqueue(callback);
}

}
23 changes: 17 additions & 6 deletions soft1614080902221/app/src/main/res/layout/activity_3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,38 @@
android:id="@+id/instructionsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="166dp"
android:layout_marginBottom="19dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:text="请按数字顺序消除中间的小方块,\n以右上角时间作为成绩!\n抓紧时间了,加油!"
android:textColor="#FF0000"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/rankListButton"
app:layout_constraintStart_toStartOf="parent" />


<Button
android:id="@+id/rankListButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:text="排行榜"
android:layout_marginBottom="11dp"
android:text="本地排行榜"
android:textColor="#0000FF"
android:textSize="30dp"
app:layout_constraintBottom_toTopOf="@+id/NetworkListButton"
app:layout_constraintStart_toStartOf="@+id/NetworkListButton" />

<Button
android:id="@+id/NetworkListButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="网络排行榜"
android:textColor="#0000FF"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/instructionsText" />
app:layout_constraintStart_toStartOf="parent" />


</android.support.constraint.ConstraintLayout>
66 changes: 66 additions & 0 deletions soft1614080902221/app/src/main/res/layout/activity_5.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity4"
tools:layout_editor_absoluteY="81dp">

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="玩家"
android:textSize="25dp" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="性别"
android:textSize="25dp" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="角色"
android:textSize="25dp" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="时间"
android:textSize="25dp" />

</LinearLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2">

</android.support.v7.widget.RecyclerView>


</android.support.constraint.ConstraintLayout>
28 changes: 28 additions & 0 deletions soft1614080902221/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
实验六
=
一.实验目的
-
掌握Android网络访问方法;

二.实验内容
-
在个人目录中创建一个表示数据的XML或JSON文件;<br>
数据文件代码提交之后从GitHub获取文件URL;<br>
在应用中通过网络编程访问GitHub的数据文件;<br>
在应用中解析并显示文件所包含的数据;<br>
将应用运行结果截图。<br>

三.实验步骤
-
1.创建一个HttpUtil.java类,里面有一个静态方法sendOkHttpRequest来发送OkHttp请求.<br>
2.在Activity5中调用HttpUtil.sendOkHttpRequest方法,传入参数address(json数据源地址),和实现Callback回调类.<br>
3.在onResponse中用JSONArray解析接收到的json数据.<br>
4.最后用runOnUiThread回到主线程更新UI,实现网络排行榜.<br>

四.实验截图
-
![](https://github.com/yangyangyang2017/android-labs-2018/blob/master/soft1614080902221/%E7%AC%AC%E5%85%AD%E6%AC%A1%E5%AE%9E%E9%AA%8C%E6%88%AA%E5%9B%BE.gif)

五.实验体会
-
1.这次实验比较简单,从自己github库中获取json并解析。要注意的是不能在子线程中更新UI,要调用runOnUiThread方法。<br>
Binary file added soft1614080902221/第六次实验截图.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions soft1614080902221/网络排行榜.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[{"playerName":"周杰伦","sex":"男","gameCharacters":"若烟","time":"1"},
{"playerName":"方文山","sex":"男","gameCharacters":"宛海","time":"3"},
{"playerName":"张韶涵","sex":"女","gameCharacters":"慕灵","time":"5"}]