Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果executet进入Web服务,Android活动将死亡_Android_Web Services_Get - Fatal编程技术网

如果executet进入Web服务,Android活动将死亡

如果executet进入Web服务,Android活动将死亡,android,web-services,get,Android,Web Services,Get,我正在尝试登录一个与RESTful Web服务通信的用户。虽然这段代码在EclipseAndroid模拟器中运行良好,但在移动设备上执行它会导致错误。更准确地说,活动消失,抛出的错误消息每次都不同 这是我的LoginActivity中的代码。单击登录按钮时执行方法tryLogin(视图v): public class LoginActivity extends Activity{ public void onCreate(Bundle icicle){ super.on

我正在尝试登录一个与RESTful Web服务通信的用户。虽然这段代码在EclipseAndroid模拟器中运行良好,但在移动设备上执行它会导致错误。更准确地说,活动消失,抛出的错误消息每次都不同

这是我的LoginActivity中的代码。单击登录按钮时执行方法
tryLogin(视图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
(...)

没有特别的描述


在这一点上,我不知道是什么导致了这个错误。在开始实现它时,我考虑使用AsyncTask来处理登录,但由于在emulator中一切正常,我也在移动设备上进行了尝试。这就是问题所在吗?为什么这样一个概念问题在模拟器上有效,而在移动设备上无效?

我认为RestHandler.executeGET(url,this);。它在UI线程中执行。使用AsyncTask或REST客户端模式

根据Android指导原则,您不应该在主UI线程上执行网络活动,否则您将获得。这是在蜂巢公司引进的。因此,如果您的模拟器是姜饼或更低,代码将工作,如果真正的设备是蜂窝或更高,它将失败。使用
AsyncTask
对您有利。

我们可以获得完整的stacktrace吗?如何从移动设备提取stacktrace?在您发布的第二个错误上方和下方应该有更多行。我理解,我的问题是如何在一种文件中获取日志,而不是手动键入all。从logcat复制并粘贴?不要使用AsyncTask执行网络请求,因为AsyncTask与启动它的活动紧密相关,因此在活动被销毁时(例如,当方向发生变化时)会导致AsyncTask被销毁。您应该改用服务模式。看看你们提到的实现GoogleIO的库
(...)
E/AndroidRuntime(12277): at imhotapp.schlettibusiness.rest.RestHandler.executeGET(...36)
E/AndroidRuntime(12277): at imhotapp.schlettibusiness.LoginActivity.tryLogin(...43)
(...)