嗨,我做了我的第一个android应用程序,它有了一个webservices,可以使用XAMPP连接到本地PC上的mysql DB,我可以轻松地从Android模拟器运行它
但是我需要从我的真实设备上运行,但是出现了与数据库的连接问题
到目前为止,我已经更改了文件的IP地址,该文件包含Web服务的连接详细信息以及eclipse中的userfunction.java(此为用户注册和登录功能)
在电话端,我的PC使用LAN,而我的电话使用connectify wireless连接到同一连接
但是当我从真实设备上单击登录按钮时,我在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;
}
}
有人可以指出任何错误或建议我解决方案吗
谢谢
最佳答案
添加以下软件包:
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();