0

i'm trying to connect my mobile application with database on server throw the url ,

at the first my code was working with emulator URL , when i replace it with server url that contains database it shows an error , (i update the php files with the database user name and password ) , please could any one help me with this :

here is the error:

05-06 07:04:08.069: E/AndroidRuntime(3350): FATAL EXCEPTION: main
05-06 07:04:08.069: E/AndroidRuntime(3350): java.lang.NullPointerException
05-06 07:04:08.069: E/AndroidRuntime(3350):     at com.example.weddingplanner.login$Login$1.run(login.java:178)
05-06 07:04:08.069: E/AndroidRuntime(3350):     at android.os.Handler.handleCallback(Handler.java:725)
05-06 07:04:08.069: E/AndroidRuntime(3350):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 07:04:08.069: E/AndroidRuntime(3350):     at android.os.Looper.loop(Looper.java:137)
05-06 07:04:08.069: E/AndroidRuntime(3350):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-06 07:04:08.069: E/AndroidRuntime(3350):     at java.lang.reflect.Method.invokeNative(Native Method)
05-06 07:04:08.069: E/AndroidRuntime(3350):     at java.lang.reflect.Method.invoke(Method.java:511)
05-06 07:04:08.069: E/AndroidRuntime(3350):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run`(ZygoteInit.java:793)`
05-06 07:04:08.069: E/AndroidRuntime(3350):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-06 07:04:08.069: E/AndroidRuntime(3350):     at dalvik.system.NativeStart.main(Native Method)

and i used this code :

package com.example.weddingplanner;

import java.util.ArrayList;
import java.util.List;


import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class login extends Activity {



private static final JSONParser jsonParser = new JSONParser();
private static final String URL_LOGIN ="http://weddingplannerg7/android_connect/login.php";

// JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_USER = "user";
    private static final String TAG_TYPE = "type";

    static String FPWEmail;


private String email;
private String password;
private String type ;
private int success ;
private ProgressDialog pDialog; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    final Button b1 = (Button) findViewById(R.id.jbutton1);//forget password
    final Button b2 = (Button) findViewById(R.id.jbutton2);//log in
    final Button b3 = (Button) findViewById(R.id.jbutton3); // new user
    final EditText t1 = (EditText) findViewById(R.id.jeditText1);//user email
    final EditText t2 = (EditText) findViewById(R.id.jeditText2);//PW


    //forget password
    b1.setOnClickListener(new OnClickListener()
    {
    public void onClick(View v)
    {
        FPWEmail =  t1.getText().toString(); 

        if (!FPWEmail.equals(""))
        {
        Intent myIntent = new Intent(login.this, forgetPW1.class);
        login.this.startActivity(myIntent);
        }else
        {
            errEmaPW("sorry try again!!");
        }
    }
    });


    //login
    b2.setOnClickListener(new OnClickListener()
    {
    public void onClick(View v)
    {

         email = t1.getText().toString(); 
         password = t2.getText().toString();

        // search if email exist in DB or not and save it in boolean
    if( email.equals("") || password.equals(""))
    {
        //errormessage
        errEmaPW("sorry try again!!");

    }else
    {
        new Login().execute();

    }
    }
    });

    //new user

    b3.setOnClickListener(new OnClickListener()
    {
    public void onClick(View v)
    {
        Intent myIntent = new Intent(login.this, newUser.class);
        login.this.startActivity(myIntent);
    }
    });



}
@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
     getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().setDisplayUseLogoEnabled(false);
        getActionBar().setHomeButtonEnabled(false);
    return true;
}

public void errEmaPW(String str)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
    builder.setMessage(str)
    .setTitle("error!")
           .setPositiveButton("okay", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) { }})
           .show();
}


/**
 * Background Async Task 
 * */
class Login extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(login.this);
        pDialog.setMessage("loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * 
     * */
    protected String doInBackground(String... params) {

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                // Check for success tag
                type = "";
                 success = 0 ;

                try {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("email", email));
                    params.add(new BasicNameValuePair("password", password));

                    // getting product details by making HTTP request
                    // Note that product details url will use GET request
                    JSONObject json = jsonParser.makeHttpRequest(
                            URL_LOGIN, "POST", params);

                    // check your log for json response
                    Log.d("Single User", json.toString());

                    // json success tag
                    success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {

                        type = json.getString(TAG_TYPE);
                        if (type.equals("user"))
                        {
                        // save user data 
                        user.email=email;
                        } else
                        {
                        admin.email=email;
                        }

                    }else
                    {
                        AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
                        builder.setMessage("sorry")
                        .setTitle("error!")
                               .setPositiveButton("okay", new DialogInterface.OnClickListener() {
                                   public void onClick(DialogInterface dialog, int id) { }})
                               .show();

                    }
                } 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 got all details
        pDialog.dismiss();

        if(success == 1)
        {
        if(type.equals("user"))
        {
            Intent myIntent = new Intent(login.this, mainUser.class);
            login.this.startActivity(myIntent);

        } else if(type.equals("admin"))
        {
            Intent myIntent = new Intent(login.this, mainAdmin.class);
            login.this.startActivity(myIntent);
        }
        }
    }
}
}

i checked the php files , i get an error in the connect statment

this is the statement cuasing the error

// include db connect class
require_once __DIR__ . '/db_connect.php';

and this is the error it self i tryed to change the directory but it doesn't work please could any one help me !!

Warning: require_once(DIR/db_connect.php) [function.require-once]: failed to open stream: No such file or directory in /home/ashjan/public_html/android_connect/login.php on line 13

Fatal error: require_once() [function.require]: Failed opening required 'DIR/db_connect.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/ashjan/public_html/android_connect/login.php on line 13

user2227683
  • 67
  • 1
  • 3

2 Answers2

0

First of all why are you performing your networking operation on UI thread, you are doing running your network code on UI inside runOnUiThread . Secondly your application is crashing due to NullPointerException at line 178. Debug your class and see what is the reason, may be you have to debug your PHP code too.

Yahya Arshad
  • 1,626
  • 2
  • 19
  • 34
  • I did so , i depug my php and this is the error show in the connect statement it said that it cannot find this directory which was working on local server (xammp) , this error just show up when i upload it on the server . I tryed so many time changing the directory just like what it said and it didn't work . Please could you help me with this error . This is the statement that cuase the error (( require_once__DIR__.'/db_connect.php')) – user2227683 May 09 '13 at 19:59
  • i don't know alot about php but if you could update your question with some more PHP snippet may be I or someone else could help you out or may be this could help you http://stackoverflow.com/questions/5371828/relative-path-in-require-once-doesnt-work – Yahya Arshad May 10 '13 at 05:39
  • thank you, the problem have been solved it because that it couldn't recognize the directory to set the connection, i replaced it with the connection statments within each php file and it works. thank you – user2227683 May 12 '13 at 14:19
0

Your db_connect.php is obviously not being found, and being that it's in a require_once the script aborts on that line without executing anything else. Changing the require_once to include_once won't fix the situation, as db_connect.php is going to be necessary to make calls to the database. You'd better find db_connect.php before anything else and make sure the code points to the correct location. Is it in the same folder? An include folder?

Lastly, to elaborate on the proper way to make calls to PHP API's from Android (or other Java apps for that matter) see here: http://developer.android.com/reference/android/os/AsyncTask.html and then have a look through Google results for "android java asynctask json"

MaKR
  • 1,882
  • 2
  • 17
  • 29
  • thank you , i remove the directory and put the connection statements at the same php file for each one and its work. yes as you said its from the connection statement it didn't recognize the directory, you've been very helpful, thank you again =) – user2227683 May 12 '13 at 14:17