0

我正在尝试将我的移动应用程序与服务器上的数据库连接,抛出 url,

起初我的代码使用模拟器 URL,当我用包含数据库的服务器 url 替换它时,它显示错误,(我用数据库用户名和密码更新 php 文件),请任何人帮助我:

这是错误:

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)

我使用了这段代码:

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);
        }
        }
    }
}
}

我检查了 php 文件,在连接语句中出现错误

这是导致错误的语句

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

这是我自己尝试更改目录但它不起作用的错误,请任何人帮助我!

警告:require_once(DIR /db_connect.php)[function.require-once]:无法打开流:第 13 行的 /home/ashjan/public_html/android_connect/login.php 中没有这样的文件或目录

致命错误:require_once() [function.require]:在 /home/ashjan 中打开所需的 ' DIR /db_connect.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') 失败/public_html/android_connect/login.php 在第 13 行

4

2 回答 2

0

首先,为什么要在 UI 线程上执行网络操作,而要在 UI 内部运行网络代码runOnUiThread。其次,由于NullPointerException第 178 行,您的应用程序崩溃了。调试您的类,看看是什么原因,可能您也必须调试您的 PHP 代码。

于 2013-05-06T07:55:43.687 回答
0

您的 db_connect.php 显然没有被找到,并且它在 require_once 中,脚本在该行中止而不执行任何其他操作。将 require_once 更改为 include_once 并不能解决问题,因为调用数据库需要 db_connect.php。您最好先找到 db_connect.php,并确保代码指向正确的位置。它在同一个文件夹中吗?包含文件夹?

最后,详细说明从 Android(或其他 Java 应用程序)调用 PHP API 的正确方法,请参见此处:http: //developer.android.com/reference/android/os/AsyncTask.html然后有查看“android java asynctask json”的 Google 结果

于 2013-05-09T22:37:58.330 回答