我正在尝试从我的Dropbox读取一个在线txt文件,并将其内容放入带有Asynctask的字符串中,但我无法设法返回我的字符串。
我的代码:
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String path ="https://dl.dropboxusercontent.com/u/29289946/PCGAMEDONWLOADER/Slots/All%20Games/Rows_available/link2.txt";
URL u = null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
runOnUiThread(new Runnable() {
@Override
public void run() {
String album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return album;
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.loadingtext);
txt.setText("Executed"); // txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
现在eclipse强调&#34;专辑&#34;接下来回来,所以我不能在进一步的操作中使用它。
感谢您的帮助。
答案 0 :(得分:2)
为什么运行UI线程将数据分配给字符串...您可以在线程中使用它。
只需在您的doInBackground
方法()中执行:
bo.write(buffer); // Write Into Buffer.
String album = bo.toString();
return album;
这里你的主要问题是你在runInMainThread()方法中本地定义了你的String变量。然后你如何进入周边地区。如果您不想更改您的程序,那么只需将全局字符串定义为:
public String doInBackGround()String...params){
String album = "";
.....
....
将值放在任何位置......例如album=bo.toString();
并返回....
答案 1 :(得分:1)
您在此功能中初始化了album
:
runOnUiThread(new Runnable() {
@Override
public void run() {
String album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
因此它仅适用于此范围。您可以通过检查Eclipse实际显示的错误来看到这一点。
答案 2 :(得分:0)
这是因为你在runOnUiThread
中声明了你的专辑字符串,所以在它之外就不会知道它。尝试在onPostExecute
中声明它,如下所示:
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String path ="https://dl.dropboxusercontent.com/u/29289946/PCGAMEDONWLOADER/Slots/All%20Games/Rows_available/link2.txt";
URL u = null;
//Declare it here as final
final String album=null;
try {
u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.connect();
InputStream in = c.getInputStream();
final ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer); // Read from Buffer.
bo.write(buffer); // Write Into Buffer.
runOnUiThread(new Runnable() {
@Override
public void run() {
album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return album;
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.loadingtext);
txt.setText("Executed"); // txt.setText(result);
// might want to change "executed" for the returned string passed
// into onPostExecute() but that is upto you
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}