ADD file via upload

master
p10297854 4 years ago
parent b15e798ae5
commit c67e86a6ff

@ -0,0 +1,105 @@
package com.example.musicwork.freeModel;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
public class CheckFileList {
public static ArrayList<String> CheckFileList(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/QueyOneFilenames",data);
try
{
myThread.start();
myThread.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
HttpURLConnection httpURLConnection=myThread.gethttpURLConnection();
Log.e("CheckFIlelist", String.valueOf(httpURLConnection));
//获取服务器上的数据
Log.e("CheckFIlelist:", "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("CheckFIlelist:", sbre.toString());
JSONObject jsonObject = new JSONObject(sbre.toString());
int size=Integer.parseInt(jsonObject.getString("size"));
Log.e("CheckFIlelist size:", String.valueOf(size));
ArrayList<String> list = new ArrayList<String>();
for(int i=0;i<size;i++){
//jsonObject.getString("filename"+i);
list.add(jsonObject.getString("filename"+i));
Log.e("CheckFIlelist:", jsonObject.getString("filename"+i));
}
return list;
}
}
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…
Cancel
Save