我正在关注使用PHP脚本和JSON将Android应用程序连接到MYSQL的教程 从这个链接 http://www.mybringback.com/tutorial-series/13193/android-mysql-php-json-part-5-developing-the-android-application/
但每次我尝试登录时应用程序都会崩溃。我尝试使用错误的凭据登录,但它会得到相同的结果
我知道这个问题已被问过几次,但没有帮助
这是报告
09-25 16:32:53.762: E/AndroidRuntime(2616): FATAL EXCEPTION: AsyncTask #2
09-25 16:32:53.762: E/AndroidRuntime(2616): Process: com.learn2crack.tab, PID: 2616
09-25 16:32:53.762: E/AndroidRuntime(2616): java.lang.RuntimeException: An error occured while executing doInBackground()
09-25 16:32:53.762: E/AndroidRuntime(2616): at android.os.AsyncTask$3.done(AsyncTask.java:300)
09-25 16:32:53.762: E/AndroidRuntime(2616): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
09-25 16:32:53.762: E/AndroidRuntime(2616): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
09-25 16:32:53.762: E/AndroidRuntime(2616): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
09-25 16:32:53.762: E/AndroidRuntime(2616): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-25 16:32:53.762: E/AndroidRuntime(2616): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-25 16:32:53.762: E/AndroidRuntime(2616): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-25 16:32:53.762: E/AndroidRuntime(2616): at java.lang.Thread.run(Thread.java:841)
09-25 16:32:53.762: E/AndroidRuntime(2616): Caused by: java.lang.NullPointerException
09-25 16:32:53.762: E/AndroidRuntime(2616): at com.learn2crack.tab.Login$AttemptLogin.doInBackground(Login.java:129)
09-25 16:32:53.762: E/AndroidRuntime(2616): at com.learn2crack.tab.Login$AttemptLogin.doInBackground(Login.java:1)
09-25 16:32:53.762: E/AndroidRuntime(2616): at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-25 16:32:53.762: E/AndroidRuntime(2616): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-25 16:32:53.762: E/AndroidRuntime(2616): ... 4 more
async
的codesnippetclass AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Login.this);
Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();
Intent i = new Intent(Login.this, MainActivity.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
使用android排球,它更快,有响应回调,运行并行线程,它使用缓存! http://developer.android.com/training/volley/index.html