I have a method called receive data which can successfully log onto MySQL database and retrieve data from database, however i am unable to return that data out of the method to display in text view on android app. When i have it running in a thread which android forces me to do i cant return a value as string and when its not in thread it wont execute on the main thread so I'm not sure how to get around this by adding it into a separate thread and also returning the string value out of the method to be used in the text view. you will see i have commented out some of my attempts to resolve so at the moment the value I'm trying to return is response.toString() which works fine until i add it to a separate thread let me know what other code elements i need to add for more information, i don't want to add it all just here and confuse the question.
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();
}
}
CodePudding user response:
I suggest you write the value you want to return to a variable. Then in your run method you call the RecieveData method. This will write the value you want to a variable "returnValue". which you can access after the thread ends.
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;
}
}
Do not create new thread in the ReceiveData method. Create the new thread from your main thread and instead of calling the receiveData function you should call the start() on the thread.
Remember to call the thread.join()
method after starting the thread before calling the getAnswer()
method. This will make the main wait for the thread to end before reading from the returnValue.
I hope this helps.
/*
Inside the main thread
*/
GetData data = new GetData("Your link here");
data.start();
data.join();
String myAnswer = data.getAnswer();