parent
d2f75e1d95
commit
6f322c8b14
@ -0,0 +1,143 @@
|
||||
package com.example.musicwork.chuangguan;
|
||||
|
||||
import android.util.JsonReader;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONStringer;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class GameService {
|
||||
public static HashMap<String , String> params = null;
|
||||
public static HashMap<String , String> CheckScore(String name) throws IOException, JSONException {
|
||||
|
||||
String data = "username=" + URLEncoder.encode(name, "utf-8");//设置数据
|
||||
MyThread_score myThread = new MyThread_score("http://10.0.2.2:8080/Manager/qeuryonescore",data);
|
||||
try
|
||||
{
|
||||
myThread.start();
|
||||
myThread.join();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
HttpURLConnection httpURLConnection=myThread.gethttpURLConnection();
|
||||
Log.e("Game:conn", String.valueOf(httpURLConnection));
|
||||
|
||||
|
||||
boolean jsonStr=myThread.getResult();
|
||||
if(jsonStr){
|
||||
int a=1;
|
||||
Log.e("Game:result", "result=true");
|
||||
}else{
|
||||
int a=1;
|
||||
Log.e("Game:result", "result=false");
|
||||
}
|
||||
if(myThread.getResult()){
|
||||
//获取服务器上的数据
|
||||
Log.e("Game:", "result start");
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
|
||||
//解码
|
||||
StringBuilder sbre = new StringBuilder();
|
||||
String line=null;
|
||||
while((line=in.readLine())!=null){
|
||||
sbre.append(line);
|
||||
sbre.append("\n");
|
||||
}
|
||||
in.close();
|
||||
Log.e("Game:", sbre.toString());
|
||||
JSONObject jsonObject = new JSONObject(sbre.toString());
|
||||
String avg = jsonObject.getString("avg");
|
||||
String max = jsonObject.getString("max");
|
||||
int rank = jsonObject.getInt("rank");
|
||||
params = new HashMap<>();
|
||||
params.put("avg", avg);
|
||||
params.put("max", max);
|
||||
params.put("rank", String.valueOf(rank));
|
||||
Log.e("avg", avg);
|
||||
Log.e("max", max);
|
||||
Log.e("rank", String.valueOf(rank));
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
|
||||
public static boolean SaveScore(String name, float score) throws IOException {
|
||||
String data = "username=" + URLEncoder.encode(name, "utf-8") + "&score=" + URLEncoder.encode(String.valueOf(score), "utf-8");//设置数据
|
||||
MyThread_score myThread = new MyThread_score("http://10.0.2.2:8080/Manager/InsertScore",data);
|
||||
try
|
||||
{
|
||||
myThread.start();
|
||||
myThread.join();
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
Log.e("result:",String.valueOf(myThread.getResult()));
|
||||
// HttpURLConnection httpURLConnection=myThread.gethttpURLConnection();
|
||||
|
||||
// boolean result = (httpURLConnection.getResponseCode() == 200);
|
||||
return myThread.getResult();
|
||||
}
|
||||
|
||||
}
|
||||
class MyThread_score extends Thread {
|
||||
private String path;
|
||||
private String data;
|
||||
private float score;
|
||||
private boolean result = false;
|
||||
HttpURLConnection httpURLConnection;
|
||||
|
||||
public MyThread_score(String path, String data) {
|
||||
this.path = path;
|
||||
this.data=data;
|
||||
}
|
||||
public MyThread_score(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
URL url = new URL(path);
|
||||
httpURLConnection = (HttpURLConnection) url.openConnection();
|
||||
httpURLConnection.setConnectTimeout(8000);//设置连接超时时间
|
||||
httpURLConnection.setReadTimeout(8000);//设置读取超时时间
|
||||
httpURLConnection.setRequestMethod("POST");//设置请求方法,post
|
||||
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置响应类型
|
||||
httpURLConnection.setRequestProperty("Content-Length", data.length() + "");//设置内容长度
|
||||
httpURLConnection.setDoOutput(true);//允许输出
|
||||
OutputStream outputStream = httpURLConnection.getOutputStream();
|
||||
outputStream.write(data.getBytes("utf-8"));//写入数据
|
||||
result = (httpURLConnection.getResponseCode() == 200);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public boolean getResult(){
|
||||
return result;
|
||||
}
|
||||
public HttpURLConnection gethttpURLConnection() {
|
||||
return httpURLConnection;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue