获取异常" java.lang.IllegalStateException:内容已被使用"同时使用Json + String

时间:2016-02-23 17:14:34

标签: android android-asynctask illegalstateexception asynctaskloader

大家好,我是Android的新手,目前正在学习android。我为我的应用程序制作登录系统。这是我的asynctask

@Override
        protected String  doInBackground(String... params) {

        BufferedReader in = null;

        ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        dataToSend.add(new BasicNameValuePair("user_email", user.email));
        dataToSend.add(new BasicNameValuePair("user_pass", user.password));
        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
        HttpClient client = new DefaultHttpClient(httpRequestParams);
        HttpPost post = new HttpPost(SERVER_ADDRESS + "login.php");
        User returnedUser= null;
        try {

            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            HttpResponse httpResponse = client.execute(post);
            HttpEntity entity = httpResponse.getEntity();

            in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            String response = "";
            String line = "";
            while ((line = in.readLine())!= null){
                response+= line;
            }
            in.close();
            Log.d("qwerty", response);

            if(response.equals("notAct\t\t")){
                return "notAct";
            }else if(response.equals("Error\t\t")){
                return "Error";
            }
            String result = EntityUtils.toString(entity);
            final JSONObject jObject = new JSONObject(result);

            if (jObject.length() == 0) {
                progressDialog.dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Connection Error json");
                builder.setPositiveButton("ok", null);
                builder.show();
            } else {
                String user_id = jObject.getString("user_id");
                String user_name = jObject.getString("user_name");
                returnedUser = new User(user.email, user.password, user_name, user_id);
                Log.d("qwerty", "Exception time");
                userCallback.done(returnedUser);
            }
        }catch(NullPointerException e){    
            e.printStackTrace();
        } catch (JSONException e){
            e.printStackTrace();
        } catch(ArithmeticException e) {
            e.printStackTrace();
        } catch (ConnectTimeoutException e) {
            Log.d("qwerty", "RunTime");
            return "Abc";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


@Override
        protected void onPostExecute(String result) {
            try {
                if (result.equals("notAct")) {
                    //account activated
                } else if (result.equals("Error")) {
                    //email does't exist
                }else if (result.equals("Abc")) {
                    //connection time out
                }
            }catch(NullPointerException e){
                //null pointer exception
            }catch (RuntimeException e){
                //
            } catch (Exception e){
                //
            }
            super.onPostExecute(result);
        }

所有异常都是handeled但是这个asynctask从php文件中获取的json和String不能一起工作

如果我在读取字符串(字符串响应)之前写入Json的编码然后json读取它,但它无法读取错误,如果用户输入了错误的用户详细信息。在此先感谢帮助。

1 个答案:

答案 0 :(得分:0)

这是正确的代码

        try {

            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            HttpResponse httpResponse = client.execute(post);
            //HttpEntity entity = httpResponse.getEntity();
            //final JSONObject jObject = new JSONObject(EntityUtils.toString(entity));

            in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            String response = "";
            String line = "";
            while ((line = in.readLine())!= null){
                response+= line;
            }
            in.close();
            Log.d("qwerty", response);

            if(response.equals("notAct\t\t")){
                return "notAct";
            }else if(response.equals("Error\t\t")){
                return "Error";
            }else {
                final JSONObject jObject = new JSONObject(response);

                if (jObject.length() == 0) {
                    progressDialog.dismiss();
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage("Connection Error json");
                    builder.setPositiveButton("ok", null);
                    builder.show();
                } else {
                    String user_id = jObject.getString("user_id");
                    String user_name = jObject.getString("user_name");
                    returnedUser = new User(user.email, user.password, user_name, user_id);
                    Log.d("qwerty", "Exception time");
                    userCallback.done(returnedUser);
                }
            }
            //String result = EntityUtils.toString(entity);

        }catch(NullPointerException e){
            e.printStackTrace();
        } catch (JSONException e){
            e.printStackTrace();
        } catch(ArithmeticException e) {
            e.printStackTrace();
        } catch (ConnectTimeoutException e) {
            Log.d("qwerty", "RunTime");
            return "Abc";
        } catch (Exception e) {
            e.printStackTrace();
        }