06-25 07:04:19.985: W/System.err(1398): org.apache.http.NoHttpResponseException: 目标服务器未能响应
06-25 07:04:20.005: W/System.err(1398): 在 org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85)
06-25 07:04:20.005: W/System.err(1398): 在 org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
06-25 07:04:20.017: W/System.err(1398): 在 org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
06-25 07:04:20.017: W/System.err(1398): 在 org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
06-25 07:04:20.017: W/System.err(1398): 在 org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
06-25 07:04:20.025: W/System.err(1398): 在 org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
06-25 07:04:20.025: W/System.err(1398): 在 org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
06-25 07:04:20.025: W/System.err(1398): 在 org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
06-25 07:04:20.035: W/System.err(1398): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-25 07:04:20.035: W/System.err(1398): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-25 07:04:20.045: W/System.err(1398): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-25 07:04:20.045: W/System.err(1398): 在 com.example.androidhive.library.JSONParser.getJSONFromUrl(JSONParser.java:47)
06-25 07:04:20.055: W/System.err(1398): 在 com.example.androidhive.library.UserFunctions.loginUser(UserFunctions.java:38)
06-25 07:04:20.055: W/System.err(1398): 在 com.example.androidhive.LoginActivity$1.onClick(LoginActivity.java:61)
06-25 07:04:20.055: W/System.err(1398): 在 android.view.View.performClick(View.java:4204)
06-25 07:04:20.065: W/System.err(1398): 在 android.view.View$PerformClick.run(View.java:17355)
06-25 07:04:20.075: W/System.err(1398): 在 android.os.Handler.handleCallback(Handler.java:725)
06-25 07:04:20.095: W/System.err(1398): 在 android.os.Handler.dispatchMessage(Handler.java:92)
06-25 07:04:20.095: W/System.err(1398): 在 android.os.Looper.loop(Looper.java:137)
06-25 07:04:20.095: W/System.err(1398): 在 android.app.ActivityThread.main(ActivityThread.java:5041)
06-25 07:04:20.115: W/System.err(1398): 在 java.lang.reflect.Method.invokeNative(Native Method)
06-25 07:04:20.115: W/System.err(1398): 在 java.lang.reflect.Method.invoke(Method.java:511)
06-25 07:04:20.125: W/System.err(1398): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 07:04:20.125: W/System.err(1398): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 07:04:20.125: W/System.err(1398): 在 dalvik.system.NativeStart.main(Native Method)
06-25 07:04:20.125: E/Buffer 错误 (1398): 转换结果时出错 java.lang.NullPointerException: lock == null
06-25 07:04:20.135: E/JSON Parser(1398): 解析数据时出错 org.json.JSONException: 在字符 0 处输入结束
06-25 07:04:20.145: D/AndroidRuntime(1398): 关闭 VM
06-25 07:04:20.145: W/dalvikvm(1398): threadid=1: 线程以未捕获的异常退出 (group=0x40a71930)
06-25 07:04:20.185:E/AndroidRuntime(1398):致命异常:主要
06-25 07:04:20.185: E/AndroidRuntime(1398): java.lang.NullPointerException
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 com.example.androidhive.LoginActivity$1.onClick(LoginActivity.java:65)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 android.view.View.performClick(View.java:4204)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 android.view.View$PerformClick.run(View.java:17355)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 android.os.Handler.handleCallback(Handler.java:725)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 android.os.Handler.dispatchMessage(Handler.java:92)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 android.os.Looper.loop(Looper.java:137)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 android.app.ActivityThread.main(ActivityThread.java:5041)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 java.lang.reflect.Method.invokeNative(Native Method)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 java.lang.reflect.Method.invoke(Method.java:511)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 07:04:20.185: E/AndroidRuntime(1398): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 07:04:20.185: E/AndroidRuntime(1398): at dalvik.system.NativeStart.main(Native Method)
登录活动.java
package com.example.androidhive;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.androidhive.library.DatabaseHandler;
import com.example.androidhive.library.UserFunctions;
public class LoginActivity extends Activity {
Button btnLogin;
Button btnLinkToRegister;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
}
}