douyi1779 2015-09-22 17:28
浏览 109
已采纳

获得正确的响应,但仍然会出现错误

I got the right response but still getting errors and its strange.

Where i press the button to go to next activity:

private void setupClickEvent() {
        signIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                userName = username.getText().toString();
                userPass = password.getText().toString();

                if (!userName.isEmpty() || !userPass.isEmpty()) {
                    accessWebService();
                    Log.e("after execute: ", String.valueOf(letLogin));
                    if (letLogin) {
                        session.createLoginSession(userName, "waiterx@gmail.com");
                        Intent intent = new Intent(getActivity().getApplicationContext(), UserProfile.class);
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
                        getActivity().finish();
                    } else {
                        Snackbar.with(getActivity().getApplicationContext()).text(getString(R.string.uname_or_password_not_found)).color(Color.parseColor("#3399FF")).show(getActivity());
                    }
                }

            }
        });
    }

    private void accessWebService() {
        task = new MyInsertDataTask();
        task.execute(new String[]{URL});
    }

UPDATED JAVA:

private class MyInsertDataTask extends AsyncTask<String, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity().getApplicationContext());
            pDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.setIndeterminate(true);
            pDialog.setMessage(getString(R.string.dialog_rate_data_submit));
            pDialog.setCancelable(false);
            pDialog.setInverseBackgroundForced(true);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            nameValuePairs = new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("user", userName));
            nameValuePairs.add(new BasicNameValuePair("pass", userPass));
            try
            {
                httpClient = new DefaultHttpClient();
                httpPost = new HttpPost(params[0]);
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpClient.execute(httpPost);
                httpEntity = response.getEntity();
                is = httpEntity.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    result.append(line);
                }
                Log.e("HTTP response: ", result.toString());

                if (result != null){
                    data = new JSONObject(result.toString());
                    isRegistered = data.getBoolean("res");}
                Log.e("isRegistered: ", String.valueOf(isRegistered));

            }
            catch(Exception e)
            {
                Log.e("Fail 1", e.toString());
            }
            return isRegistered;
        }
        @Override
        protected void onPostExecute(Boolean aVoid) {
            super.onPostExecute(aVoid);
            pDialog.dismiss();
        }
    }

And still does not logging me in!!!!

The logcat output:

09-23 04:36:48.627  22163-22163/com.order.app.order E/after execute:﹕ false
09-23 04:36:48.659  22163-22182/com.order.app.order W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-23 04:36:48.659  22163-22182/com.order.app.order W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xabc9aa60, error=EGL_SUCCESS
09-23 04:36:51.240  22163-22188/com.order.app.order E/HTTP response:﹕ {"res":true}
09-23 04:36:51.240  22163-22188/com.order.app.order E/isRegistered:﹕ true
09-23 04:36:53.777  22163-22163/com.order.app.order E/after execute:﹕ true
09-23 04:36:53.812  22163-22182/com.order.app.order W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-23 04:36:53.812  22163-22182/com.order.app.order W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa1d555c0, error=EGL_SUCCESS
09-23 04:36:53.815  22163-22185/com.order.app.order E/HTTP response:﹕ {"res":true}
09-23 04:36:53.815  22163-22185/com.order.app.order E/isRegistered:﹕ true

Since the responce is true and i give isRegistered the value from the data object why is it still troubles me? The First time i click the button the letLogin variable is false and the Second time is true. How is this possible? Anyone would be able to help me appreciate it!!!

  • 写回答

3条回答 默认 最新

  • dorflv5944 2015-09-23 09:12
    关注

    Ok i found it at last why it needed 2 times to press the button. 1 to execute and 1 to confirm for correct data.

    The code:

    private class MyInsertDataTask extends AsyncTask<String, Void, Boolean> {
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(getActivity().getApplicationContext());
                pDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pDialog.setIndeterminate(true);
                pDialog.setMessage(getString(R.string.dialog_rate_data_submit));
                pDialog.setCancelable(false);
                pDialog.setInverseBackgroundForced(true);
                pDialog.show();
            }
    
    
            @Override
            protected Boolean doInBackground(String... params) {
                nameValuePairs = new ArrayList<>();
                nameValuePairs.add(new BasicNameValuePair("user", userName));
                nameValuePairs.add(new BasicNameValuePair("pass", userPass));
                try {
                    httpClient = new DefaultHttpClient();
                    httpPost = new HttpPost(params[0]);
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    response = httpClient.execute(httpPost);
                    httpEntity = response.getEntity();
                    is = httpEntity.getContent();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    StringBuilder result = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        result.append(line);
                    }
                    Log.e("HTTP response: ", result.toString());
    
                    if (result != null) {
                        data = new JSONObject(result.toString());
                        isRegistered = data.getBoolean("res");
                    }
                    Log.e("isRegistered: ", String.valueOf(isRegistered));
                } catch (Exception e) {
                    Log.e("Fail 1", e.toString());
                }
                return isRegistered;
            }
    
            @Override
            protected void onPostExecute(Boolean aVoid) {
                super.onPostExecute(aVoid);
                pDialog.dismiss();
                if (aVoid) {
                    session.createLoginSession(userName, "waiterx@gmail.com");
                    Intent intent = new Intent(getActivity().getApplicationContext(), UserProfile.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
                    getActivity().finish();
                }else {
                    Snackbar.with(getActivity().getApplicationContext()).text(getString(R.string.uname_or_password_not_found)).color(Color.parseColor("#3399FF")).show(getActivity());
                }
            }
        }
    

    And the execution:

    private void setupClickEvent() {
            signIn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    userName = username.getText().toString();
                    userPass = password.getText().toString();
                    if (!userName.isEmpty() || !userPass.isEmpty()) {
                        accessWebService();
                    }
    
                }
            });
        }
    
        private void accessWebService() {
            task = new MyInsertDataTask();
            task.execute(new String[]{URL});
        }
    

    Works like a charm. Thank you all for your time!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B