0

I am coding for the first time in Android and I was developing a Login page. There is no error while running the app but the app gives me "Incorrect User details" message even if I am entering correct details. After debugging the code, I figured out that the error is while executing this line.

OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());

I set up different breakpoints and figured out that as soon as this line executes, I get the error message.

Here is my complete code:

    public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
        User user;
        GetUserCallback userCallback;

        public fetchUserDataAsyncTask(User user, GetUserCallback userCallback) {
            this.user = user;
            this.userCallback = userCallback;
        }

        @Override
        protected User doInBackground(Void... params) {

            Map<String, String> dataToSend = new HashMap<>();

            dataToSend.put("username", user.username);
            dataToSend.put("password", user.password);

            String encodedStr = getEncodedData(dataToSend);

            BufferedReader reader = null;

            User returnedUser = null;

            try{
                //Converting the address string into URL
                URL url = new URL(SERVER_ADDRESS + "FetchUserData.php");

                //Opening the connection
                 HttpURLConnection con = (HttpURLConnection) url.openConnection();

                con.setRequestMethod("POST");
                con.setDoOutput(true);
                OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());

                writer.write(encodedStr);
                writer.flush();

                StringBuilder sb = new StringBuilder();
                reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String line;
                while((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                line = sb.toString();

                Log.i("custom check","The values received are:");
                Log.i("custom check",line);

                JSONObject jobject = new JSONObject(line);

                if(jobject.length() == 0){
                    returnedUser = null;
                }else{
                    String fname = jobject.getString("FirstName");
                    String lname = jobject.getString("LastName");

                    returnedUser = new User(fname, lname, user.username, user.password);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(reader != null) {
                    try{
                        reader.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }

            return returnedUser;
        }

The problem is that "returnedUser" is always null even if I enter correct credentials and that is why it is showing error message.

Kashish
  • 84
  • 1
  • 11
  • What's `e.getMessage();` report? –  Nov 19 '15 at 01:11
  • So I am getting this error: `E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab75a6f0` in Logcat. – Kashish Nov 19 '15 at 02:00
  • Might want to see [this question](http://stackoverflow.com/questions/32561479/android-studio-getslotfrombufferlocked-unknown-buffer-error), then. –  Nov 19 '15 at 02:07
  • Thank you for the link. How do I change API level for my app just to test it for Lollipop and see if it works there. – Kashish Nov 19 '15 at 02:20
  • Check out [this search](https://www.google.com/search?q=android+change+api+level) –  Nov 19 '15 at 02:43
  • I changed the `targetSdkVersion` of my app from 23 to 22 (Lollipop), but it gave me an error saying that I need to uninstall and reinstall the app and that would lose my application data. Wanna check if I am doing it the correct way. – Kashish Nov 19 '15 at 06:02
  • I'd suggest you post that as another question with full details. –  Nov 19 '15 at 15:42

0 Answers0