I am attempting to run an open source application on my computer that connects an android application to a WAMP server.
This application definitely works as a colleague of mine can run it on their computer. However, I cannot get it to run on mine.
The following code is from an activity that attempts to register a user so they can then log in. However I get a logcat error when I attempt to run it. What is the issue?
I have connected my WAMP server correctly (Icon is green) and also created the database within MYSQL. I know that this is working as I can populate the MYSQL database from a PHP script. However I cannot seem to connect it to an android application.
Any help would be greatly appreciated, I have tried all that I can think of in order to solve it.
Logcat Error report 1:
07-24 13:41:17.127: E/AndroidRuntime(1355): FATAL EXCEPTION: AsyncTask #1
07-24 13:41:17.127: E/AndroidRuntime(1355): Process: com.example.mysqltest, PID: 1355
07-24 13:41:17.127: E/AndroidRuntime(1355): java.lang.RuntimeException: An error occured while executing doInBackground()
07-24 13:41:17.127: E/AndroidRuntime(1355): at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-24 13:41:17.127: E/AndroidRuntime(1355): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.lang.Thread.run(Thread.java:841)
07-24 13:41:17.127: E/AndroidRuntime(1355): Caused by: java.lang.NullPointerException
07-24 13:41:17.127: E/AndroidRuntime(1355): at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:109)
07-24 13:41:17.127: E/AndroidRuntime(1355): at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:1)
07-24 13:41:17.127: E/AndroidRuntime(1355): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-24 13:41:17.127: E/AndroidRuntime(1355): ... 4 more
Logcat error 2:
07-24 13:41:17.127: E/AndroidRuntime(1355): FATAL EXCEPTION: AsyncTask #1
07-24 13:41:17.127: E/AndroidRuntime(1355): Process: com.example.mysqltest, PID: 1355
07-24 13:41:17.127: E/AndroidRuntime(1355): java.lang.RuntimeException: An error occured while executing doInBackground()
07-24 13:41:17.127: E/AndroidRuntime(1355): at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-24 13:41:17.127: E/AndroidRuntime(1355): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.lang.Thread.run(Thread.java:841)
07-24 13:41:17.127: E/AndroidRuntime(1355): Caused by: java.lang.NullPointerException
07-24 13:41:17.127: E/AndroidRuntime(1355): at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:109)
07-24 13:41:17.127: E/AndroidRuntime(1355): at com.example.mysqltest.Register$CreateUser.doInBackground(Register.java:1)
07-24 13:41:17.127: E/AndroidRuntime(1355): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-24 13:41:17.127: E/AndroidRuntime(1355): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-24 13:41:17.127: E/AndroidRuntime(1355): ... 4 more
07-24 13:41:17.699: E/WindowManager(1355): android.view.WindowLeaked: Activity com.example.mysqltest.Register has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52906bd4 V.E..... R......D 0,0-1026,288} that was originally added here
07-24 13:41:17.699: E/WindowManager(1355): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
07-24 13:41:17.699: E/WindowManager(1355): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
07-24 13:41:17.699: E/WindowManager(1355): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
07-24 13:41:17.699: E/WindowManager(1355): at android.app.Dialog.show(Dialog.java:286)
07-24 13:41:17.699: E/WindowManager(1355): at com.example.mysqltest.Register$CreateUser.onPreExecute(Register.java:86)
07-24 13:41:17.699: E/WindowManager(1355): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
07-24 13:41:17.699: E/WindowManager(1355): at android.os.AsyncTask.execute(AsyncTask.java:535)
07-24 13:41:17.699: E/WindowManager(1355): at com.example.mysqltest.Register.onClick(Register.java:68)
07-24 13:41:17.699: E/WindowManager(1355): at android.view.View.performClick(View.java:4438)
07-24 13:41:17.699: E/WindowManager(1355): at android.view.View$PerformClick.run(View.java:18422)
07-24 13:41:17.699: E/WindowManager(1355): at android.os.Handler.handleCallback(Handler.java:733)
07-24 13:41:17.699: E/WindowManager(1355): at android.os.Handler.dispatchMessage(Handler.java:95)
07-24 13:41:17.699: E/WindowManager(1355): at android.os.Looper.loop(Looper.java:136)
07-24 13:41:17.699: E/WindowManager(1355): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-24 13:41:17.699: E/WindowManager(1355): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 13:41:17.699: E/WindowManager(1355): at java.lang.reflect.Method.invoke(Method.java:515)
07-24 13:41:17.699: E/WindowManager(1355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-24 13:41:17.699: E/WindowManager(1355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-24 13:41:17.699: E/WindowManager(1355): at dalvik.system.NativeStart.main(Native Method)
Android activity:
public class Register extends Activity implements OnClickListener{
private EditText user, pass;
private Button mRegister;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/register.php";
//testing on Emulator:
private static final String LOGIN_URL = "http://192.168.56.1/webservice/register.php";
//testing from a real server:
//private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/register.php";
//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
user = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
mRegister = (Button)findViewById(R.id.register);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new CreateUser().execute();
}
class CreateUser extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Register.this);
pDialog.setMessage("Creating User...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
//Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// full json response
Log.d("Login attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("User Created!", json.toString());
finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Register.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
Corresponding PHP script:
<?php
/*
Our "config.inc.php" file connects to database every time we include or require
it within a php script. Since we want this script to add a new user to our db,
we will be talking with our database, and therefore,
let's require the connection to happen:
*/
require("config.inc.php");
//if posted data is not empty
if (!empty($_POST)) {
//If the username or password is empty when the user submits
//the form, the page will die.
//Using die isn't a very good practice, you may want to look into
//displaying an error message within the form instead.
//We could also do front-end form validation from within our Android App,
//but it is good to have a have the back-end code do a double check.
if (empty($_POST['username']) || empty($_POST['password'])) {
// Create some data that will be the JSON response
$response["success"] = 0;
$response["message"] = "Please Enter Both a Username and Password.";
//die will kill the page and not execute any code below, it will also
//display the parameter... in this case the JSON data our Android
//app will parse
die(json_encode($response));
}
//if the page hasn't died, we will check with our database to see if there is
//already a user with the username specificed in the form. ":user" is just
//a blank variable that we will change before we execute the query. We
//do it this way to increase security, and defend against sql injections
$query = " SELECT 1 FROM users WHERE username = :user";
//now lets update what :user should be
$query_params = array(
':user' => $_POST['username']
);
//Now let's make run the query:
try {
// These two statements run the query against your database table.
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one to product JSON data:
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
//fetch is an array of returned data. If any data is returned,
//we know that the username is already in use, so we murder our
//page
$row = $stmt->fetch();
if ($row) {
// For testing, you could use a die and message.
//die("This username is already in use");
//You could comment out the above die and use this one:
$response["success"] = 0;
$response["message"] = "I'm sorry, this username is already in use";
die(json_encode($response));
}
//If we have made it here without dying, then we are in the clear to
//create a new user. Let's setup our new query to create a user.
//Again, to protect against sql injects, user tokens such as :user and :pass
$query = "INSERT INTO users ( username, password ) VALUES ( :user, :pass ) ";
//Again, we need to update our tokens with the actual data:
$query_params = array(
':user' => $_POST['username'],
':pass' => $_POST['password']
);
//time to run our query, and create the user
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error2. Please Try Again!";
die(json_encode($response));
}
//If we have made it this far without dying, we have successfully added
//a new user to our database. We could do a few things here, such as
//redirect to the login page. Instead we are going to echo out some
//json data that will be read by the Android application, which will login
//the user (or redirect to a different activity, I'm not sure yet..)
$response["success"] = 1;
$response["message"] = "Username Successfully Added!";
echo json_encode($response);
//for a php webservice you could do a simple redirect and die.
//header("Location: login.php");
//die("Redirecting to login.php");
} else {
?>
<h1>Register</h1>
<form action="register.php" method="post">
Username:<br />
<input type="text" name="username" value="" />
<br /><br />
Password:<br />
<input type="password" name="password" value="" />
<br /><br />
<input type="submit" value="Register New User" />
</form>
<?php
}
?>
Logcat error:
07-24 19:16:30.584: E/BandwidthController(155): runIptablesCmd(): res=1 status=768 failed /system/bin/ip6tables -t raw -A bw_raw_PREROUTING -m owner --socket-exists
07-24 19:16:30.584: A/NetworkManagementService(554): problem enabling bandwidth controls
07-24 19:16:30.584: A/NetworkManagementService(554): com.android.server.NativeDaemonConnector$NativeDaemonFailureException: command '1 bandwidth enable' failed with '400 1 Bandwidth command failed'
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:391)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.server.NativeDaemonConnector.executeForList(NativeDaemonConnector.java:327)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.server.NativeDaemonConnector.execute(NativeDaemonConnector.java:292)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.server.NetworkManagementService.prepareNativeDaemon(NetworkManagementService.java:348)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.server.NetworkManagementService.systemReady(NetworkManagementService.java:224)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.server.ServerThread$2.run(SystemServer.java:961)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.server.am.ActivityManagerService.systemReady(ActivityManagerService.java:9363)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.server.ServerThread.initAndLoop(SystemServer.java:938)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.server.SystemServer.main(SystemServer.java:1179)
07-24 19:16:30.584: A/NetworkManagementService(554): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 19:16:30.584: A/NetworkManagementService(554): at java.lang.reflect.Method.invoke(Method.java:515)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-24 19:16:30.584: A/NetworkManagementService(554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-24 19:16:30.584: A/NetworkManagementService(554): at dalvik.system.NativeStart.main(Native Method)
07-24 19:16:30.588: E/WifiStateMachine(554): Failed to reload STA firmware java.lang.IllegalArgumentException: command '2 softap fwreload eth1 STA' failed with '501 2 SoftAP command has failed'