2013-05-06 94 views
0

我想要我的移动应用程序与数据库服务器连接抛出网址retrive数据,错误尝试从服务器

在第一代码才能使用仿真器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文件,我在连接statment

这是cuasing错误

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

的声明得到一个错误,这是错误它的自我我tryed改变目录,但它不工作,请任何人可以帮助我!

警告:require_once(DIR /db_connect.php)[function.require一次]:未能打开流:在/home/ashjan/public_html/android_connect/login.php没有这样的文件或目录第13行

致命错误:require_once()[function.require]:无法开口所需 'DIR /db_connect.php'(include_path中=':/ usr/lib中/ PHP:在/ usr /本地/ LIB/PHP的)in /home/ashjan/public_html/android_connect/login.php on line 13

+0

Log.d(“Single User”,json.toString());你的json对象为空 – 2013-05-06 07:41:32

+0

你在哪个行号出错? – QuokMoon 2013-05-06 08:40:55

回答

0

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

+0

我这样做,我depug我的PHP,这是连接语句中的错误显示它说,它无法找到这个目录,这是在本地服务器(xammp)工作,这个错误只是当我上传它在服务器上显示。我尝试了很多次改变目录,就像它所说的那样,它不起作用。请你能帮我解决这个错误。这是声明错误((require_once__DIR __。'/ db_connect.php')) – user2227683 2013-05-09 19:59:38

+0

我不知道很多关于PHP,但如果你可以用更多的PHP代码片段更新你的问题可能是我或其他人可以帮助你或者可能是这可以帮助你http:// stackoverflow。com/questions/5371828/relative-path-in-require-once-doesnt-work – 2013-05-10 05:39:35

+0

谢谢,问题已经解决了,因为它无法识别目录来设置连接,我用连接状态替换它在每个PHP文件中,它的作品。谢谢 – user2227683 2013-05-12 14:19:13

0

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

最后,阐述了正确的方式,利用Android的到PHP API的调用(或其他Java应用程序为此事)在这里看到:http://developer.android.com/reference/android/os/AsyncTask.html,然后必须通过谷歌搜索结果为“机器人的java的AsyncTask JSON”

看看
+0

谢谢你,我删除了目录,并把连接语句放在每个人和它的工作相同的php文件中。是的,正如你从连接声明中所说的那样,它不能识别目录,你一直很有帮助,再次感谢你=) – user2227683 2013-05-12 14:17:01