我正在尝试登录与 RESTful 网络服务通信的用户。虽然此代码在 eclipse Android 模拟器中运行良好,但在移动设备上执行它会导致错误。更准确地说,Acitivity 死亡,每次都抛出不同的错误消息。
这是我的 LoginActivity 中的代码。tryLogin(View v)
单击登录按钮时执行该方法:
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();
}
}
RestHandler 中的函数executeGET(...)
如下:
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;
}
如前所述,特别是抛出了两条错误消息:
(...)
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
(...)
和
(...)
E/AndroidRuntime(12277): at imhotapp.schlettibusiness.rest.RestHandler.executeGET(...36)
E/AndroidRuntime(12277): at imhotapp.schlettibusiness.LoginActivity.tryLogin(...43)
(...)
没有特别的描述。
在这一点上,我不知道是什么导致了这个错误。当开始实现它时,我考虑使用 AsyncTask 来处理登录,但由于在模拟器中一切正常,我也在移动设备上尝试了它。这是问题吗?为什么这种概念性问题在模拟器上有效,而在移动设备上无效?