0

I am trying to get this project running on my development pc. But when I am trying to register a new user this is the error that I am being thrown by the logcat (attached is the logcat error log for the session)

This is the tutorial that I am following,

http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

       11-03 11:56:24.685: D/dalvikvm(2279): GC_FOR_ALLOC freed 206K, 9% free 3763K/4096K, paused 2ms, total 6ms
11-03 11:56:24.925: E/JSON(2279): <br />
11-03 11:56:24.925: E/JSON(2279): <font size='1'><table class='xdebug-error xe-deprecated' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
11-03 11:56:24.925: E/JSON(2279): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp\www\ah_login_api\include\DB_Connect.php on line <i>18</i></th></tr>
11-03 11:56:24.925: E/JSON(2279): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
11-03 11:56:24.925: E/JSON(2279): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
11-03 11:56:24.925: E/JSON(2279): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>252816</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\ah_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
11-03 11:56:24.925: E/JSON(2279): <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>276616</td><td bgcolor='#eeeeec'>DB_Functions->__construct(  )</td><td title='C:\wamp\www\ah_login_api\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>19</td></tr>
11-03 11:56:24.925: E/JSON(2279): <tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>282712</td><td bgcolor='#eeeeec'>DB_Connect->connect(  )</td><td title='C:\wamp\www\ah_login_api\include\DB_Functions.php' bgcolor='#eeeeec'>..\DB_Functions.php<b>:</b>13</td></tr>
11-03 11:56:24.925: E/JSON(2279): <tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>283528</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-connect' target='_new'>mysql_connect</a>
11-03 11:56:24.925: E/JSON(2279): (  )</td><td title='C:\wamp\www\ah_login_api\include\DB_Connect.php' bgcolor='#eeeeec'>..\DB_Connect.php<b>:</b>18</td></tr>
11-03 11:56:24.925: E/JSON(2279): </table></font>
11-03 11:56:24.925: E/JSON(2279): {"tag":"register","success":1,"error":0,"uid":"5457b44f108d33.70669064","user":{"name":"fdgdf","email":"dfgfd@gmail.com","created_at":"2014-11-04 00:58:55","updated_at":null}}
11-03 11:56:24.925: E/JSON Parser(2279): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
11-03 11:56:24.935: D/AndroidRuntime(2279): Shutting down VM
11-03 11:56:24.935: W/dalvikvm(2279): threadid=1: thread exiting with uncaught exception (group=0xb2d12b20)
11-03 11:56:24.935: E/AndroidRuntime(2279): FATAL EXCEPTION: main
11-03 11:56:24.935: E/AndroidRuntime(2279): Process: com.example.androidhive, PID: 2279
11-03 11:56:24.935: E/AndroidRuntime(2279): java.lang.NullPointerException
11-03 11:56:24.935: E/AndroidRuntime(2279):     at com.example.androidhive.RegisterActivity$1.onClick(RegisterActivity.java:66)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at android.view.View.performClick(View.java:4438)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at android.view.View$PerformClick.run(View.java:18422)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at android.os.Handler.handleCallback(Handler.java:733)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at android.os.Looper.loop(Looper.java:136)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at java.lang.reflect.Method.invoke(Method.java:515)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-03 11:56:24.935: E/AndroidRuntime(2279):     at dalvik.system.NativeStart.main(Native Method)

What am I missing? Btw, I already created all the necessary database and entered the user and the password. Just to add on that when I am trying to access the index.php that I downloaded from the above site thru localhost it is saying the webpage is not available, although I believe it is only as a start point for JSON and not actually a index page.. correct me if I am wrong.

JackyBoi
  • 2,695
  • 12
  • 42
  • 74
  • Look at the "caused by" - its the most important thing in the log cat. You get NullPointerException on your onClick. Please post your onClick code. – Blaze Tama Nov 02 '14 at 14:12
  • The error is in the link which you are trying to open. Tt shows certain error message. The output of the link is not a json string. Please check this link in browser. – Sabin Jose Nov 02 '14 at 14:22
  • **Read the error**! mysql_connect(): Access denied for user 'root'@'localhost' (using password: NO) in C:\wamp\www\ah_login_api\include\DB_Connect.php – SLaks Nov 02 '14 at 14:35
  • Sorry couldn't get back earlier. So I corrected that but still facing issue. Please see the edited log above in my original questions – JackyBoi Nov 03 '14 at 15:19
  • "localhost" refers to the android device itself, as it can only ever mean the machine on which a program is running. – Chris Stratton Nov 04 '14 at 02:09

1 Answers1

1

Your JSONObject.getJSONObject(String) call throws a JSONException due to what appears to be malformed input.


Make the code silently move on

Using JSONObject.optJSONObject(String) should instead return a null rather than failing. You can then handle the null value as a special case. This would be preferable over your current behaviour as applications should be as silent about bad input as possible and should not fail.

Imagine being on a network with a captive web portal requiring the user to log in or agree to terms. Such an example would crash your app at the current state due to malformed input.

Invalid data coming from any untrusted source (including any file on shared storage, or data coming through just about any network connections) is considered expected and should not trigger any logging at a level higher then DEBUG when it's detected to be invalid (and even then logging should be as limited as possible).

by Google from Log Sparingly (Code Style Guidelines for Contributors)


Fix up the server's output

As this is a development environment and you are also controlling the server-side code, you also need to fix the server output. Your configuration is currently trying to connect to the local machine as the root user without a password which is (thankfully) invalid.

You want to either:

  • Switch to a different, limited access user (recommended)
  • Set the root user's password (advised against, due to security concerns)
Valter Jansons
  • 3,904
  • 1
  • 22
  • 25
  • where do you reckon i should do that? also this time round I did not input null values. – JackyBoi Nov 03 '14 at 15:34
  • Pardon, but which part are you talking about exactly when asking where to do something? – Valter Jansons Nov 03 '14 at 15:55
  • the part where u mentioned "Using JSONObject.optJSONObject(String) should instead return a null rather than failing" from the above given link in my question I downloaded the project, where should I handle this exception? – JackyBoi Nov 03 '14 at 15:58
  • On line 66 in RegisterActivity you have the offending `JSONObject json_user = json.getJSONObject("user");` which is what fails right now. I am talking about changing the `get` there to `opt` and handling the possible null value right after that. – Valter Jansons Nov 03 '14 at 16:08
  • Changed that line and still getting this error 11-03 11:56:24.925: E/JSON Parser(2279): Error parsing data org.json.JSONException: Value
    – JackyBoi Nov 03 '14 at 17:00
  • Btw sorry to ask, did u try running the project sucessfully ur self? – JackyBoi Nov 03 '14 at 17:01
  • I have not run the project, but it is still complaining about unparsable input. You should really work on server-side DB auth right now and provide it the correct uname/pass combo as fixing that will make it work as expected. The hardening bit should follow after that. – Valter Jansons Nov 03 '14 at 17:06
  • I have changed the user name and password, I also have updated my latest logcat in my original question after change uname/pass and also your suggestion. – JackyBoi Nov 04 '14 at 00:55
  • Your server is still complaining about using deprecated features. You might want to either rewrite the serverside code to use mysqli or PDO or you could just suppress the warnings for now. – Valter Jansons Nov 04 '14 at 05:57
  • 1
    Right now according to the log in the post, you have an exception during JSON parsing, followed by a NPE being thrown, causing the app to crash. The parsing problem happens due to your server outputting a warning about using deprecated features on the page. If you were to fix that, the parsing problem would go away. Please open the API endpoint/site in a browser to see the warning in its full glory. – Valter Jansons Nov 04 '14 at 13:28
  • Yes that did the trick! I changed it to pdo, the link explaining more about pdo http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/20767765#20767765 – JackyBoi Nov 04 '14 at 14:57