The problem is that I expect the text that I read from a webpage calling grabURL
is absolutely equal to what I put into String lorem
in the onPostExecute
.
But Android says: "No, it does not!"
Why do I think Android is not right about equality?
1) StackTrace screenshot:
2) Screenphoto:
3) You can find String lorem = "Lorem ipsum dolor sit amet";
downward in my class, and look through the source code of the webpage I'm reading text from:
Source code of my classes:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grabURL("http://www.cyberweb.pp.ua/x/o.html");
}
public void grabURL(String url) {
new GrabURL().execute(url);
}
private class GrabURL extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
protected Void doInBackground(String... urls) {
try {
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
String lorem = "Lorem ipsum dolor sit amet";
if (Error != null) {
Toast.makeText(MainActivity.this, Error, Toast.LENGTH_LONG)
.show();
} else {
TextView xyu = (TextView) findViewById(R.id.xyu);
TextView xyu1 = (TextView) findViewById(R.id.xyu1);
TextView xyu2 = (TextView) findViewById(R.id.xyu2);
xyu.setText(Content);
xyu1.setText(lorem);
Log.v("lorem", lorem);
Log.v("Content", Content);
String eq = null;
if (Content.length() != lorem.length()) {
Log.v("length?", "Different length");
}
if (Content.trim().equalsIgnoreCase(lorem.trim())) {
xyu2.setText("EQUAL");
eq = "EQUAL";
Log.v("equal?", eq);
}
else {
xyu2.setText("NOT EQUAL");
eq = "NOT EQUAL";
Log.v("equal?", eq);
}
}
}
}
}
This issue is driving me nuts.
Сan anybody guess what happens? Thank you in advance!
Last Stacktrace:
The issue was caused by webpage encoding. Equality is correct when changing the encoding to UTF-8
.