1. Are you ready for the Galaxy S20? Here is everything we know so far!

Login tutorial app crashes

Discussion in 'Android Development' started by Dannnorman, Jul 23, 2017.

  1. Dannnorman

    Dannnorman Lurker
    Thread Starter

    Hi guys, I am new to android and I am trying to develop my first app, I started with this tutorial and got to the step #4 so far


    I got to the point of interaction between the app and the database but here something happens, specifically when I click the registerHere link, the app crashes and I don't know whats going on. I have tried it in my phone and also in the virtual nexus 5 and on both is the same result.
    The Host provider I am using is https://profreehost.com/ there I created a SQL table.
    Please if you can give me your support to see how can I fix this issue. Thanks!

    The register request.java
    Code (Text):
    1.  
    2. package com.example.android.loginregister;
    3.  
    4. import com.android.volley.Response;
    5. import com.android.volley.toolbox.StringRequest;
    6.  
    7. import java.util.HashMap;
    8. import java.util.Map;
    9.  
    10. /**
    11. * Created by DeadMarshes on 7/21/2017.
    12. */
    13.  
    14. public class RegisterRequest extends StringRequest {
    15.     private static final String REGISTER_REQUEST_URL = "http://danielandnorman.unaux.com/Register.php";
    16.     private Map<String, String> params;
    17.     public RegisterRequest(String name, String username, int age, String password, Response.Listener<String> listener){
    18.         super(Method.POST, REGISTER_REQUEST_URL, listener, null);
    19.         params = new HashMap<>();
    20.         params.put("name", name);
    21.         params.put("username", username);
    22.         params.put("password", password);
    23.         params.put("age", age + "");
    24.     }
    25.  
    26.     [USER=1021285]@override[/USER]
    27.     public Map<String, String> getParams() {
    28.         return params;
    29.     }
    30.  

    For the register activity.Java

    Code (Text):
    1.  
    2. public class RegisterActivity extends AppCompatActivity {
    3.  
    4.     [USER=1021285]@override[/USER]
    5.     protected void onCreate(Bundle savedInstanceState) {
    6.         super.onCreate(savedInstanceState);
    7.         setContentView(R.layout.activity_register);
    8.         final EditText etAge = (EditText) findViewById(R.id.etAge);
    9.         final EditText etName = (EditText) findViewById(R.id.etName);
    10.         final EditText etUsername = (EditText) findViewById(R.id.etUsername);
    11.         final EditText etPassword = (EditText) findViewById(R.id.etPassword);
    12.         final Button bRegister = (Button) findViewById(R.id.bRegister);
    13.  
    14.         bRegister.setOnClickListener(new View.OnClickListener() {
    15.             [USER=1021285]@override[/USER]
    16.             public void onClick(View view) {
    17.                 final String name = etName.getText().toString();
    18.                 final String username = etUsername.getText().toString();
    19.                 final String password = etPassword.getText().toString();
    20.                 final int age = Integer.parseInt(etAge.getText().toString());
    21.  
    22.                 Response.Listener<String> responseListener = new Response.Listener<String>() {
    23.                     [USER=1021285]@override[/USER]
    24.                     public void onResponse(String response) {
    25.                         try {
    26.                             JSONObject jsonResponse = new JSONObject(response);
    27.                             boolean success = jsonResponse.getBoolean("success");
    28.                             if (success) {
    29.                                 Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
    30.                                 RegisterActivity.this.startActivity(intent);
    31.                             } else{
    32.                                 AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
    33.                                 builder.setMessage("Register Failed")
    34.                                         .setNegativeButton("Retry", null)
    35.                                         .create()
    36.                                         .show();
    37.                             }
    38.  
    39.                         } catch (JSONException e) {
    40.                             e.printStackTrace();
    41.                         }
    42.                     }
    43.                 };
    44.  
    45.  
    46.  
    47.                 RegisterRequest registerRequest = new RegisterRequest(name, username,age, password, responseListener );
    48.                 RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
    49.                 queue.add(registerRequest);
    50.             }
    51.         });
    52.     }
    53. }
    54.  

    Also the php file Register.php

    Code (Text):
    1.  
    2. <?php
    3.     $con = mysqli_connect("sql111.unaux.com", "username", "password", "table");
    4.  
    5.     $name = $_POST["name"];
    6.     $age = $_POST["age"];
    7.     $username = $_POST["username"];
    8.     $password = $_POST["password"];
    9.     $statement = mysqli_prepare($con, "INSERT INTO UserTable (name, username, age, password) VALUES (?, ?, ?, ?)");
    10.     mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);
    11.     mysqli_stmt_execute($statement);
    12.  
    13.     $response = array();
    14.     $response["success"] = true;
    15.  
    16.     echo json_encode($response);
    17. ?>
    18.  


     


    #1 Dannnorman, Jul 23, 2017
    Last edited by a moderator: Jul 24, 2017
    Rob likes this.
  2. Best Answer:
    Post #7 by Deleted User, Jul 24, 2017 (3 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. wseng92

    wseng92 Android Enthusiast

    Please provide the stacktrace and also format your code with code tags
     
    Bg260 likes this.
  4. Dannnorman

    Dannnorman Lurker
    Thread Starter

    Hi , there is the Logcat, is that what you need?
    How do I post my code in blocks in the forum?
    Thank you for your reply

    Code (Text):
    1.  
    2.  
    3. 07-23 20:29:38.198 7679-7679/com.example.android.loginregister E/AndroidRuntime: FATAL EXCEPTION: main
    4.                                                                                  Process: com.example.android.loginregister, PID: 7679
    5.                                                                                  java.lang.NumberFormatException: For input string: ""
    6.                                                                                      at java.lang.Integer.parseInt(Integer.java:620)
    7.                                                                                      at java.lang.Integer.parseInt(Integer.java:643)
    8.                                                                                      at com.example.android.loginregister.RegisterActivity$1.onClick(RegisterActivity.java:36)
    9.                                                                                      at android.view.View.performClick(View.java:6219)
    10.                                                                                      at android.view.View$PerformClick.run(View.java:24482)
    11.                                                                                      at android.os.Handler.handleCallback(Handler.java:769)
    12.                                                                                      at android.os.Handler.dispatchMessage(Handler.java:98)
    13.                                                                                      at android.os.Looper.loop(Looper.java:164)
    14.                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6540)
    15.                                                                                      at java.lang.reflect.Method.invoke(Native Method)
    16.                                                                                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    17.                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
    18.  
    19.  
     
    #3 Dannnorman, Jul 24, 2017
    Last edited: Jul 24, 2017
  5. wseng92

    wseng92 Android Enthusiast

    Add CODE Tags between your code.

    The error is quite clear. You only allow to enter number in age editText (etAge), but you enter String.
     
    #4 wseng92, Jul 24, 2017
    Last edited: Jul 24, 2017
    Dannnorman likes this.
  6. wseng92

    wseng92 Android Enthusiast

    Have you read https://androidforums.com/threads/please-read-me-before-posting.987318/ before posting ?

     
    #5 wseng92, Jul 24, 2017
    Last edited: Jul 24, 2017
  7. Deleted User

    Deleted User Guest

    I edited your original post to add the code tags. To explain further - if you post your code like this it should be nicely formatted


    [code]
    // Paste some code in here
    [/code]


    To elaborate on the above answer as to why you get the problem with your app, it looks like what you typed into the 'etAge' EditText was not a proper number. So when the code tried to convert whatever you typed into a numeric value, it couldn't e.g. the String "hello" can not be converted to a value. Does this make sense?
     
    Dannnorman and wseng92 like this.
  8. Deleted User

    Deleted User Guest

    Actually looking at the stack trace, it's clear that you typed nothing into the EditText. So parseInt() method tries to convert a blank string "" into a number. That's what caused your exception.

    Code (Text):
    1.  
    2. java.lang.NumberFormatException: For input string: ""
    3.  
    To catch this error, you could add some defensive checking code, to detect either a null value, or blank String, and display an error message to the user.
     
    Dannnorman likes this.
  9. Dannnorman

    Dannnorman Lurker
    Thread Starter

    Thank you very much for your help,
    It makes total sense, I didn't realize that was making extra trouble when left blank, so fixing that issue I get now the following, looks like a problem with the request

    Code (Text):
    1.  
    2.  
    3. 07-24 22:24:08.078 13365-13365/com.example.android.loginregister I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3d264d85 time:45296072
    4. 07-24 22:24:11.878 13365-13365/com.example.android.loginregister D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
    5. 07-24 22:24:12.018 13365-22010/com.example.android.loginregister I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
    6. 07-24 22:24:12.028 13365-22010/com.example.android.loginregister I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
    7. 07-24 22:24:12.028 13365-22010/com.example.android.loginregister I/qtaguid: Tagging socket 37 with tag d3987bb100000000{3549985713,0} uid -1, pid: 13365, getuid(): 10239
    8. 07-24 22:24:12.508 13365-22010/com.example.android.loginregister I/qtaguid: Untagging socket 37
    9. 07-24 22:24:12.538 13365-13365/com.example.android.loginregister W/System.err: org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
    10. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
    11. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
    12. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
    13. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at com.example.android.loginregister.RegisterActivity$1$1.onResponse(RegisterActivity.java:42)
    14. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at com.example.android.loginregister.RegisterActivity$1$1.onResponse(RegisterActivity.java:38)
    15. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
    16. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
    17. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
    18. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
    19. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
    20. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at android.os.Looper.loop(Looper.java:145)
    21. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5835)
    22. 07-24 22:24:12.548 13365-13365/com.example.android.loginregister W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    23. 07-24 22:24:12.558 13365-13365/com.example.android.loginregister W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
    24. 07-24 22:24:12.558 13365-13365/com.example.android.loginregister W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
    25. 07-24 22:24:12.558 13365-13365/com.example.android.loginregister W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
    26. 07-24 22:24:15.418 13365-13365/com.example.android.loginregister D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
    27. 07-24 22:24:22.028 13365-13365/com.example.android.loginregister D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
    28. 07-24 22:24:22.188 13365-22163/com.example.android.loginregister I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
    29. 07-24 22:24:22.188 13365-22163/com.example.android.loginregister I/qtaguid: Tagging socket 37 with tag d3987bb100000000{3549985713,0} uid -1, pid: 13365, getuid(): 10239
    30. 07-24 22:24:22.198 13365-22163/com.example.android.loginregister I/qtaguid: Untagging socket 37
    31. 07-24 22:24:22.198 13365-22163/com.example.android.loginregister E/Volley: [35745] BasicNetwork.performRequest: Unexpected response code 400 for http://danielandnorman.unaux.com/Register.php
    32.  
    33.  
    Do you know what this may be due to?
     
  10. Deleted User

    Deleted User Guest

    You would be wise to take some time to understand how the request is being made to your remote PHP service. I don't see the code which is making the HTTP request, but I think it could be in class StringRequest, which you have not shown.
    So the parameter passed in to your onResponse() method should be a JSON String, but it isn't

    Code (Text):
    1.  
    2. public void onResponse(String response) {
    3.  
    It looks like the HTML response String. You then try to construct a JSON object using this String

    Code (Text):
    1.  
    2. JSONObject jsonResponse = new JSONObject(response);
    3.  
    Which causes the exception because it's not valid JSON.
    If you don't know what JSON is, then now would be a good time to read up about that.
    Bottom line is that response needs to be a valid JSON String.
     
  11. Dannnorman

    Dannnorman Lurker
    Thread Starter


    HI @LV426,
    Isn't the request file "Register request.java"?
    I am trying to get to know this JSON method better, and checking how I can modify to be a valid JSON string. I appreciate your help
     
  12. Deleted User

    Deleted User Guest

    Well RegisterRequest doesn't do very much. It extends StringRequest, which I presume is doing all the heavy lifting work by calling the remote service via a HTTP request. It also probably invokes your listener callback method - onResponse(). It's that code we need to see.
     
  13. Dannnorman

    Dannnorman Lurker
    Thread Starter

    ok, you mean this

    String request.java
    Code (Text):
    1.  
    2. /*
    3. * Copyright (C) 2011 The Android Open Source Project
    4. *
    5. * Licensed under the Apache License, Version 2.0 (the "License");
    6. * you may not use this file except in compliance with the License.
    7. * You may obtain a copy of the License at
    8. *
    9. *      http://www.apache.org/licenses/LICENSE-2.0
    10. *
    11. * Unless required by applicable law or agreed to in writing, software
    12. * distributed under the License is distributed on an "AS IS" BASIS,
    13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14. * See the License for the specific language governing permissions and
    15. * limitations under the License.
    16. */
    17.  
    18. package com.android.volley.toolbox;
    19.  
    20. import com.android.volley.NetworkResponse;
    21. import com.android.volley.Request;
    22. import com.android.volley.Response;
    23. import com.android.volley.Response.ErrorListener;
    24. import com.android.volley.Response.Listener;
    25.  
    26. import java.io.UnsupportedEncodingException;
    27.  
    28. /**
    29. * A canned request for retrieving the response body at a given URL as a String.
    30. */
    31. public class StringRequest extends Request<String> {
    32.     private final Listener<String> mListener;
    33.  
    34.     /**
    35.      * Creates a new request with the given method.
    36.      *
    37.      * @param method the request {@link Method} to use
    38.      * @param url URL to fetch the string at
    39.      * @param listener Listener to receive the String response
    40.      * @param errorListener Error listener, or null to ignore errors
    41.      */
    42.     public StringRequest(int method, String url, Listener<String> listener,
    43.             ErrorListener errorListener) {
    44.         super(method, url, errorListener);
    45.         mListener = listener;
    46.     }
    47.  
    48.     /**
    49.      * Creates a new GET request.
    50.      *
    51.      * @param url URL to fetch the string at
    52.      * @param listener Listener to receive the String response
    53.      * @param errorListener Error listener, or null to ignore errors
    54.      */
    55.     public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
    56.         this(Method.GET, url, listener, errorListener);
    57.     }
    58.  
    59.     @Override
    60.     protected void deliverResponse(String response) {
    61.         mListener.onResponse(response);
    62.     }
    63.  
    64.     @Override
    65.     protected Response<String> parseNetworkResponse(NetworkResponse response) {
    66.         String parsed;
    67.         try {
    68.             parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
    69.         } catch (UnsupportedEncodingException e) {
    70.             parsed = new String(response.data);
    71.         }
    72.         return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    73.     }
    74. }
    75.  
     
  14. Deleted User

    Deleted User Guest

    I think you should find a better tutorial. This one isn't very good and hasn't really explained what's happening. It would take too long for me to explain things fully in this forum.
     
  15. Dannnorman

    Dannnorman Lurker
    Thread Starter

    OK thank you very much for your support
     
  16. amanwaa

    amanwaa Lurker

    Thank you for the whole content, it's really helpful for all newbies
     
Loading...

Share This Page

Loading...