I am sending a some json to a php page from my java application which echos "finished" if successful. The json and php are working great. I am trying to pass a string to a thread and then change the value of the string to the echo from the php in the thread, and when the Thread is finished I want to use an if statement to determine if the URL connection was successfully completed... which it is I just can't get the value of the string from the Thread.
here is my code:
main.java
final String line = "unfinished";
Thread iURL = new instrURL(line, jsonArray);
iURL.start();
while(iURL.isAlive())
{
System.out.println("In wait loop");
}
System.out.println(line);
if(line.trim() == "finished")
{
System.out.println("Made it to finished");
}
else
{
System.out.println("Did not make it to finished");
}
instrURL.java
public class instrURL extends Thread{
String line;
String jsonArray;
public instrURL(String line, String jsonArray)
{
this.line = line;
this.jsonArray = jsonArray;
}
public void run()
{
try
{
URL url = new URL("http://fake.php?jsonArray="+URLEncoder.encode(jsonArray, "UTF-8"));
URLConnection conn = url.openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
line = rd.readLine();
System.out.println(line);
rd.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
the console:
In wait while loop In wait while loop ... ... ... In wait while loop finished In wait while loop In wait while loop In wait while loop unfinished Did not make it to finished
As you can see from the console the Thread gets the finished, but once outside of the Thread the strings value is still unfinished.
Any help would be greatly appreciated.