1

嗨,我已经制作了我的第一个 android 应用程序,它有一个 web 服务,可以使用 XAMPP 连接到我本地 PC 中的 mysql DB 我可以从 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)

这是保存用户 reg 位置地址的 userfunction.java。并登录网络服务器

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

}

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

谢谢

4

1 回答 1

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();
于 2013-05-20T04:38:17.663 回答