我正在尝试将我的移动应用程序与服务器上的数据库连接,然后抛出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]:无法打开流:/ home / ashjan / public_html / android_connect / login中没有此类文件或目录。第13行的PHP
致命错误:require_once()[function.require]:无法打开所需的' DIR /db_connect.php'(include_path ='。:/ usr / lib / php:/ usr / local /第13行/home/ashjan/public_html/android_connect/login.php中的lib / php')
答案 0 :(得分:0)
首先,为什么要在UI线程上执行网络操作,您正在runOnUiThread
内的UI上运行网络代码。其次,由于NullPointerException
在第178行,您的应用程序崩溃了。调试您的类并查看原因,您可能也需要调试PHP代码。
答案 1 :(得分: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然后查看Google搜索结果“android java asynctask json “