Доброго вечера!
Столкнулся с проблемой. В AsyncTask, в методе onPostExecute некорректно срабатывает оператор if. Тоесть результат сравнения в условии должен быть true, а он false, несмотря на то, что он должен выдавать True
Код:
Код:
package com.gilevich.library;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
String[] books, desc, img, free, full, name;
DownloadFile downloadFile;
ProgressBar p1;
int ct = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
p1 = (ProgressBar) findViewById(R.id.progressBar1);
downloadFile = new DownloadFile();
downloadFile.execute("http://lib.gilevich.net/Books.txt");
}
void get_info() {
BufferedReader br;
for (int i = 0; i < books.length; i++) {
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream("/sdcard/" + get_name(books[i])),
"Cp1251"));
String str;
int a = 0;
while ((str = br.readLine()) != null) {
switch (a) {
case 0:
name[i] = str;
break;
case 1:
desc[i] = str;
break;
case 2:
img[i] = str;
break;
case 3:
free[i] = str;
break;
case 4:
full[i] = str;
break;
}
a++;
}
br.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
p1.setVisibility(4);
}
void get_books() {
BufferedReader br;
try {
Log.d("i1", "pre2");
br = new BufferedReader(new InputStreamReader(new FileInputStream(
"/sdcard/Books.txt"), "Cp1251"));
Log.d("i1", "pre3");
String str;
int i = 0;
while ((str = br.readLine()) != null) {
i++;
}
Log.d("i1", "pre3");
books = new String[i];
desc = new String[i];
img = new String[i];
free = new String[i];
full = new String[i];
name = new String[i];
Log.d("i1", "pre3");
for (int j = 0; j < i; j++) {
books[j] = br.readLine();
downloadFile.execute(br.readLine());
}
br.close();
Log.d("i1", "pre5");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// ret_res();
}
String get_name(String path) {
String val = path;
return (val.substring(val.lastIndexOf('/') + 1, val.length()));
}
}
class DownloadFile extends AsyncTask<String, Integer, String> {
MainActivity ma = new MainActivity();
String cur_file;
@Override
protected String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
cur_file = sUrl[0];
Log.d("i1", cur_file);
Log.d("i1", ma.get_name(sUrl[0]));
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/"
+ ma.get_name(sUrl[0]));
Log.d("i1", "precre");
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
Log.d("i1", "aft");
output.flush();
output.close();
input.close();
Log.d("i1", "aftall");
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
File file = new File(cur_file);
cur_file = file.getName();
Log.d("i1", "'" + cur_file + "'");
//тут срабатывает неправильно if (cur_file == "Books.txt") {
Log.d("i1", "post");
ma.get_books();
} else {
Log.d("i1", "true");
ma.ct++;
Log.d("i1", "inc");
if (ma.ct == ma.books.length) {
Log.d("i1", "inc");
ma.get_info();
}
Log.d("i1", "succ");
}
}
}
Вот лог:
11-26 17:28:19.444:
http://lib.gilevich.net/Books.txt
11-26 17:28:19.645: Books.txt
11-26 17:28:20.015: precre
11-26 17:28:20.025: aft
11-26 17:28:20.025: aftall
11-26 17:28:20.025: 'Books.txt'
11-26 17:28:20.025: true
11-26 17:28:20.025: inc
Почему он неправильно выдаёт результат сравнения?