Android 为什么可以';我不能从异步任务返回字符串吗?
我试图从Dropbox中读取一个联机txt文件,并使用Asynctask将其内容放入字符串中,但我无法返回字符串 我的代码:Android 为什么可以';我不能从异步任务返回字符串吗?,android,eclipse,android-asynctask,Android,Eclipse,Android Asynctask,我试图从Dropbox中读取一个联机txt文件,并使用Asynctask将其内容放入字符串中,但我无法返回字符串 我的代码: new LongOperation().execute(""); } private class LongOperation extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) {
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) {}
}
}
new LongOperation().execute(“”);
}
私有类LongOperation扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串路径=”https://dl.dropboxusercontent.com/u/29289946/PCGAMEDONWLOADER/Slots/All%20Games/Rows_available/link2.txt";
URL u=null;
试一试{
u=新URL(路径);
HttpURLConnection c=(HttpURLConnection)u.openConnection();
c、 setRequestMethod(“GET”);
c、 connect();
InputStream in=c.getInputStream();
final ByteArrayOutputStream bo=新ByteArrayOutputStream();
字节[]缓冲区=新字节[1024];
in.read(buffer);//从缓冲区读取。
写入(缓冲区);//写入缓冲区。
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
String album=bo.toString();
试一试{
bo.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
}捕获(格式错误){
e、 printStackTrace();
}捕获(协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回相册;
}
@凌驾
受保护的void onPostExecute(字符串结果){
TextView txt=(TextView)findViewById(R.id.loadingtext);
txt.setText(“已执行”);//txt.setText(结果);
//可能需要更改传递的返回字符串的“已执行”
//进入onPostExecute(),但这取决于您
}
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的void onProgressUpdate(void…值){}
}
}
现在eclipse在return旁边的“album”下加了下划线,所以我不能在进一步的操作中使用它
感谢您的帮助。您已在此功能中初始化了相册:
runOnUiThread(new Runnable() {
@Override
public void run() {
String album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
因此,它仅在此范围内可用。您可以通过检查Eclipse实际显示的错误来了解这一点。您在该函数中初始化了
album
:
runOnUiThread(new Runnable() {
@Override
public void run() {
String album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
因此,它仅在此范围内可用。您可以通过检查Eclipse实际显示的错误来了解这一点。您在该函数中初始化了
album
:
runOnUiThread(new Runnable() {
@Override
public void run() {
String album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
因此,它仅在此范围内可用。您可以通过检查Eclipse实际显示的错误来了解这一点。您在该函数中初始化了
album
:
runOnUiThread(new Runnable() {
@Override
public void run() {
String album = bo.toString();
try {
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
因此,它仅在此范围内可用。您可以通过检查Eclipse实际显示的错误来了解这一点。这是因为您在
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) {}
}
私有类LongOperation扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串路径=”https://dl.dropboxusercontent.com/u/29289946/PCGAMEDONWLOADER/Slots/All%20Games/Rows_available/link2.txt";
URL u=null;
//在此宣布它为最终版本
最终字符串album=null;
试一试{
u=新URL(路径);
HttpURLConnection c=(HttpURLConnection)u.openConnection();
c、 setRequestMethod(“GET”);
c、 connect();
InputStream in=c.getInputStream();
final ByteArrayOutputStream bo=新ByteArrayOutputStream();
字节[]缓冲区=新字节[1024];
in.read(buffer);//从缓冲区读取。
写入(缓冲区);//写入缓冲区。
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
album=bo.toString();
试一试{
bo.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
}捕获(格式错误){
e、 printStackTrace();
}捕获(协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回相册;
}
@凌驾
受保护的void onPostExecute(字符串结果){
TextView txt=(TextView)findViewById(R.id.loadingtext);
txt.setText(“已执行”);//txt.setText(结果);
//可能需要更改传递的返回字符串的“已执行”
//进入onPostExecute(),但这取决于您
}
@凌驾
受保护的void onPreExecute(){}
@凌驾
受保护的void onProgressUpdate(void…值){}
}
这是因为您在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) {}
}
私有类LongOperation扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串路径=”https://dl.dropboxusercontent.com/u/29289946/PCGAMEDONWLOADER/Slots/All%20Games/Rows_available/link2.txt";
URL u=null;
//在此宣布它为最终版本
最终字符串album=null;
试一试{
u=新URL(路径);
HttpURLConnection c=(HttpURLConnection)u.openConnection();
c、 setRequestMethod(“GET”);
c、 connect();
InputStream in=c.getInputStream();
final ByteArrayOutputStream bo=新ByteArrayOutputStream();
字节[]缓冲区=新字节[1024];
in.read(buffer);//从缓冲区读取。
写入(缓冲区);//写入缓冲区。
runOnUiThread(新的Runnable(){
@凌驾
公共空间