-1

Hello guys am new to android and currently learning android. Am making login system for my app. Here is my 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);
        }

All exceptions are handeled but json and String which are taken from a php file by this asynctask is not working together

if I write the coding of Json before Reading String (String Response) then json Read by it but it is unable to read the error like if user entered the incorrect user details. Thanks in advance for Helping.

  • `EntityUtils.toString(entity)` + `in.readLine()` where (`in` consuming the entity) ... so what you expected? ... do you know that you are using different code for doing the same thing twice? – Selvin Feb 23 '16 at 17:17
  • no coz am new in android and did't get what exactly you are saying about **EntityUtils.toString(entity) + in.readLine() where (in consuming the entity) ** – Honey Kamboj Feb 23 '16 at 17:22
  • use only `String result = EntityUtils.toString(entity);` and get rid of the `while` loop. Once you read through the InputStream coming from the Http call once, you can't do it again. That's what Selvin meant – Blackbelt Feb 23 '16 at 17:22
  • Thank lot **Selvin** and **Blackbelt** now its working thanks again .... :-) – Honey Kamboj Feb 23 '16 at 17:27

1 Answers1

0

Here is the Correct Code

        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();
        }