Reputation: 5052
I'm trying to log in an user communicating with a RESTful webservice. While this code works fine in the eclipse Android emulator, executing it on a mobile device results in an error. More precisely, the Acitivity dies, throwing error messages that vary each time.
This is the code in my LoginActivity. The method tryLogin(View v)
is executed when clicked on the login button:
public class LoginActivity extends Activity{
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.s_login);
}
public void tryLogin(View v){
EditText un = (EditText) findViewById(R.id.et_li_username);
EditText pw = (EditText) findViewById(R.id.et_li_password);
String username = un.getText().toString();
String password = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(pw.getText().toString().getBytes(), 0, pw.getText().toString().length());
password = new BigInteger(1, md.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
ToastHandler.showToast(e.getMessage(), this);
}
ToastHandler.writeToLog("now exevuting get");
String url = RestHandler.REST_URL + "arg=0&un=" + username + "&pw=" + password + "&" + RestHandler.API_KEY;
String[] response = RestHandler.executeGET(url, this); // Line 43
String formattedResponse = "";
if(response != null && response[1] != null){
ToastHandler.writeToLog(response[1]);
if(response[0].equals("json")){
}else{
String temp = response[1].trim();
formattedResponse = temp.substring(1, temp.length()-1);
}
}else{
ToastHandler.showToast("Fehler beim Einloggen!", this);
}
if(formattedResponse == "Livia" || formattedResponse == "Luki" || formattedResponse.equals("Maki")
|| formattedResponse=="Nadine" || formattedResponse=="Roberto"){
loginUser(formattedResponse);
}else{
ToastHandler.showToast("Falsche Benutzerangaben!", this);
}
}
private void loginUser(String username){
Intent intent = new Intent(this, MenuActivity.class);
intent.putExtra("username", username);
startActivity(intent);
//finish();
}
}
The function executeGET(...)
from RestHandler is as follows:
public static String[] executeGET(String url, Context context){
HttpGet get = new HttpGet(url);
HttpResponse response;
String[] result = new String[2];
Log.i("url", url);
try{
response = CLIENT.execute(get); // Line 36
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream in = entity.getContent();
result[1] = convertStreamToString(in, context);
JsonElement jsonElem = new JsonParser().parse(result[1]);
if(jsonElem.isJsonArray()) {
result[0] = "json";
} else {
result[0] = "nok";
}
in.close();
}
}catch (ClientProtocolException e){
ToastHandler.showToast(e.getMessage(), context);
}catch (IOException e){
ToastHandler.showToast(e.getMessage(), context);
}catch (JsonSyntaxException e){
ToastHandler.showToast(e.getMessage(), context);
}
return result;
}
As already mentioned, there are in particular two error messages, that are throwed:
(...)
W/InputDispatcher(1985): Consumer closed input channel or an error occurred
E/InputDispatcher(1985): channel '...' ~ Channel is unrecoverably broken and will be disposed!
W/InputDispatcher(1985): Attempted to unregister already unregistered input channel
(...)
and
(...)
E/AndroidRuntime(12277): at imhotapp.schlettibusiness.rest.RestHandler.executeGET(...36)
E/AndroidRuntime(12277): at imhotapp.schlettibusiness.LoginActivity.tryLogin(...43)
(...)
with no particular description.
At this point, I have no idea what causes this error. When started to implement it, I considered to use an AsyncTask to process the login, but as everything worked fine in the emulator, I tried it on a mobile device too. Is this the problem? Why does such a conceptional issue works on the emulator but not on a mobile device?
Upvotes: 1
Views: 150
Reputation: 5919
As per Android guidelines, you are not supposed to do network activity on the main UI thread, else you get NetworkOnMainThreadException
. This was introduced in Honeycomb. So if your emulator is Gingerbread or lower, the code will work, and if the real device is Honeycomb or higher, it will fail. Use AsyncTask
to your benefit.
Upvotes: 1
Reputation: 1047
I think that the problem in RestHandler.executeGET(url, this);. It execute in UI thread. Use AsyncTask or REST client pattern http://www.google.com/events/io/2010/sessions/developing-RESTful-android-apps.html
Upvotes: 1