我有一个名为receive data的方法,它可以成功地登录到MySQL数据库并从数据库中检索数据。然而我无法返回数据的方法在文本视图显示在android应用程序。当我有它在一个线程运行android迫使我做我不能返回一个值作为字符串,当它不是线程不会在主线程上执行所以我不确定如何绕过这个通过添加它到一个单独的线程并返回字符串值的方法用于文本视图。你会看到我已经注释掉了一些我试图解决的问题我试图返回的值是response.toString(),它工作得很好,直到我把它添加到一个单独的线程,让我知道我需要添加更多信息的其他代码元素,我不想把它全部添加到这里,混淆问题。
package com.example.scottysmith;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class GetData extends Thread {
public String RecieveData(String link) {
GetData thread = new GetData();
thread.start();
// Thread outputdata = new Thread(() -> {
// try {
URL url = null;
try {
url = new URL(link);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection urlConnection = null;
try {
assert url != null;
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
//urlConnection.setDoOutput(true);
assert urlConnection != null;
urlConnection.setDoInput(true);
// urlConnection.setRequestProperty("Content-Type", "application/json");
//urlConnection.setRequestProperty("Accept", "application/json");
//urlConnection.setRequestProperty("Content- Type","application/x-www-form-urlencoded");
//urlConnection.setRequestProperty("charset", "UTF-8");
//urlConnection.connect();
//urlConnection.getOutputStream();
// urlConnection.getResponseCode();
try {
urlConnection.setRequestMethod("GET");
} catch (ProtocolException e) {
e.printStackTrace();
}
/*JSONObject data = new JSONObject();
data.put("name", name);
data.put("surname", surname);
data.put("location", location);
OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
wr.write(String.valueOf(data));
wr.flush();
wr.close();*/
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
String inputLine = null;
StringBuilder response = new StringBuilder();
while (true) {
try {
assert in != null;
if ((inputLine = in.readLine()) == null) break;
} catch (IOException e) {
e.printStackTrace();
}
response.append(inputLine);
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//return response.toString();
System.out.println(response);
// } catch (Exception e) {
// e.printStackTrace();
// }
// });
// }
// );
// outputdata.start();
return response.toString();
}
}
我建议您将要返回的值写入变量。然后在run方法中调用RecieveData方法。这将把你想要的值写入一个变量&;returnvalue&;。可以在线程结束后访问。
public class GetData extends Thread {
private String returnValue;
private String link;
public GetData(String link){
this.link = link;
}
private void RecieveData() {
//some code here;
//Do not create new Thread here.
returnValue = response.toString();
}
@Override
public void run(){
RecieveData();
}
public String getAnswer(){
return returnValue;
}
}
不要在ReceiveData方法中创建新线程。从主线程创建新线程,而不是调用receiveData函数,你应该调用线程上的start()。
后记得要调用thread.join()
方法调用getAnswer()
方法之前启动线程。这将使主线程在读取returnValue之前等待线程结束。我希望这对你有帮助。
/*
Inside the main thread
*/
GetData data = new GetData("Your link here");
data.start();
data.join();
String myAnswer = data.getAnswer();