不幸的是,从真实设备运行时,android应用程序停止了

时间:2013-05-18 04:37:54

标签: android mysql web-services xampp

嗨我已经制作了我的第一个Android应用程序,它有一个webservices连接到我的本地PC上的mysql数据库使用XAMPP我可以从Android模拟器完美运行它没有麻烦 但我需要从我的真实设备运行,但我遇到了与数据库连接的问题

到目前为止,我已经更改了文件中的IP地址,其中包含Web服务的连接详细信息以及eclipse中的userfunction.java(这个用户注册和登录功能)

在手机端,我的电脑使用局域网,我的手机使用连接无线连接到同一个连接

但是当我从我的真实设备点击登录按钮时,我在我的logcat

中得到了这个
05-18 12:19:40.600: E/SensorManager(13663): thread start
05-18 12:19:40.725: E/SpannableStringBuilder(13663): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-18 12:19:40.725: E/SpannableStringBuilder(13663): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-18 12:19:47.695: E/JSON Parser(13663): Error parsing data org.json.JSONException: End of input at character 0 of 
05-18 12:19:47.700: E/AndroidRuntime(13663): FATAL EXCEPTION: main
05-18 12:19:47.700: E/AndroidRuntime(13663): java.lang.NullPointerException
05-18 12:19:47.700: E/AndroidRuntime(13663):    at com.mohammed.watzIslam.LoginActivity$1.onClick(LoginActivity.java:62)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.view.View.performClick(View.java:4211)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.view.View$PerformClick.run(View.java:17267)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.os.Handler.handleCallback(Handler.java:615)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.os.Looper.loop(Looper.java:137)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at android.app.ActivityThread.main(ActivityThread.java:4898)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at java.lang.reflect.Method.invokeNative(Native Method)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at java.lang.reflect.Method.invoke(Method.java:511)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-18 12:19:47.700: E/AndroidRuntime(13663):    at dalvik.system.NativeStart.main(Native Method)

这是userfunction.java,用于保存用户reg的位置地址。并登录webserives

\
public class UserFunctions {

private JSONParser jsonParser;

private static String loginURL = "http://xxxx:port#/android_login_api/";
private static String registerURL = "http://xxxx:port#/android_login_api/";

private static String login_tag = "login";
private static String register_tag = "register";

// constructor
public UserFunctions(){
jsonParser = new JSONParser();
}

/**
* function make Login Request
* @param email
* @param password
* */
public JSONObject loginUser(String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
// Log.e("JSON", json.toString());
return json;
}

/**
* function make reg. Request
* @param name
* @param email
* @param password
* */
public JSONObject registerUser(String name, String password, String email){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));



// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
// return json
return json;
}

/**
* Function get Login status
* */
public boolean isUserLoggedIn(Context context){
DatabaseHandler db = new DatabaseHandler(context);
int count = db.getRowCount();
if(count > 0){
// user logged in
return true;
}
return false;
}

/**
* Function to logout user
* Reset Database
* */
public boolean logoutUser(Context context){
DatabaseHandler db = new DatabaseHandler(context);
db.resetTables();
return true;
}

}

有人可以指出任何错误或建议我解决方案

谢谢

1 个答案:

答案 0 :(得分:0)

添加这些包:

import android.app.ProgressDialog;
import android.os.AsyncTask;

将json声明为类成员字段:

私有JSONObject json;

替换JSONObject json = jsonParser.getJSONFromUrl(loginURL,params); 有了这个:

new AsyncTask<Void, Void, Void>() {
    ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(SplashActivity.this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Loading, please wait...");
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... pms) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        json = jsonParser.getJSONFromUrl(loginURL, params);
        return null;
    }
    protected void onPostExecute(Void result) {
        if(progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        if(json != null) {
            Log.e("JSON", json.toString());
        }
        else {
            Log.e("JSON", "JSON is NULL");
        }
    };
}.execute();