我需要从线程中获取字符串值,但我不明白!全局变量不起作用!你能帮我吗?
这是我的代码。我需要使用dataString
:
public class Deserializable {
public void execute() {
new Thread() {
public void run() {
String surl = "http://myaddressxxxxx";
HttpURLConnection urlConnection = null;
URL url = null;
try {
url = new URL(surl);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
int b = in.read();
List<Byte> bytes = new LinkedList<Byte>();
while (b != -1) {
bytes.add((byte) b);
b = in.read();
}
byte[] array = new byte[bytes.size()];
for (int i = 0; i < bytes.size(); i++) {
array[i] = bytes.get(i).byteValue();
}
// I need return this String.
String dataString = new String(array);
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
}
}.start();
}