06-25 07:04:19.985: W/System.err(1398): org.apache.http.NoHttpResponseException: The target server failed to respond 06-25 07:04:20.005: W/System.err(1398): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:85) 06-25 07:04:20.005: W/System.err(1398): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174) 06-25 07:04:20.017: W/System.err(1398): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180) 06-25 07:04:20.017: W/System.err(1398): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235) 06-25 07:04:20.017: W/System.err(1398): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259) 06-25 07:04:20.025: W/System.err(1398): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279) 06-25 07:04:20.025: W/System.err(1398): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121) 06-25 07:04:20.025: W/System.err(1398): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428) 06-25 07:04:20.035: W/System.err(1398): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 06-25 07:04:20.035: W/System.err(1398): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 06-25 07:04:20.045: W/System.err(1398): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 06-25 07:04:20.045: W/System.err(1398): at com.example.androidhive.library.JSONParser.getJSONFromUrl(JSONParser.java:47) 06-25 07:04:20.055: W/System.err(1398): at com.example.androidhive.library.UserFunctions.loginUser(UserFunctions.java:38) 06-25 07:04:20.055: W/System.err(1398): at com.example.androidhive.LoginActivity$1.onClick(LoginActivity.java:61) 06-25 07:04:20.055: W/System.err(1398): at android.view.View.performClick(View.java:4204) 06-25 07:04:20.065: W/System.err(1398): at android.view.View$PerformClick.run(View.java:17355) 06-25 07:04:20.075: W/System.err(1398): at android.os.Handler.handleCallback(Handler.java:725) 06-25 07:04:20.095: W/System.err(1398): at android.os.Handler.dispatchMessage(Handler.java:92) 06-25 07:04:20.095: W/System.err(1398): at android.os.Looper.loop(Looper.java:137) 06-25 07:04:20.095: W/System.err(1398): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-25 07:04:20.115: W/System.err(1398): at java.lang.reflect.Method.invokeNative(Native Method) 06-25 07:04:20.115: W/System.err(1398): at java.lang.reflect.Method.invoke(Method.java:511) 06-25 07:04:20.125: W/System.err(1398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-25 07:04:20.125: W/System.err(1398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 06-25 07:04:20.125: W/System.err(1398): at dalvik.system.NativeStart.main(Native Method) 06-25 07:04:20.125: E/Buffer Error(1398): Error converting result java.lang.NullPointerException: lock == null 06-25 07:04:20.135: E/JSON Parser(1398): Error parsing data org.json.JSONException: End of input at character 0 of 06-25 07:04:20.145: D/AndroidRuntime(1398): Shutting down VM 06-25 07:04:20.145: W/dalvikvm(1398): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 06-25 07:04:20.185: E/AndroidRuntime(1398): FATAL EXCEPTION: main 06-25 07:04:20.185: E/AndroidRuntime(1398): java.lang.NullPointerException 06-25 07:04:20.185: E/AndroidRuntime(1398): at com.example.androidhive.LoginActivity$1.onClick(LoginActivity.java:65) 06-25 07:04:20.185: E/AndroidRuntime(1398): at android.view.View.performClick(View.java:4204) 06-25 07:04:20.185: E/AndroidRuntime(1398): at android.view.View$PerformClick.run(View.java:17355) 06-25 07:04:20.185: E/AndroidRuntime(1398): at android.os.Handler.handleCallback(Handler.java:725) 06-25 07:04:20.185: E/AndroidRuntime(1398): at android.os.Handler.dispatchMessage(Handler.java:92) 06-25 07:04:20.185: E/AndroidRuntime(1398): at android.os.Looper.loop(Looper.java:137) 06-25 07:04:20.185: E/AndroidRuntime(1398): at android.app.ActivityThread.main(ActivityThread.java:5041) 06-25 07:04:20.185: E/AndroidRuntime(1398): at java.lang.reflect.Method.invokeNative(Native Method) 06-25 07:04:20.185: E/AndroidRuntime(1398): at java.lang.reflect.Method.invoke(Method.java:511) 06-25 07:04:20.185: E/AndroidRuntime(1398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 06-25 07:04:20.185: E/AndroidRuntime(1398): at 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)
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();
}
});
}
}
答案 0 :(得分:1)
正如一些人所指出的那样,在第65行的LoginActivity中,你试图访问一个空对象;这是一个例外。您应该查看您在该行上执行的操作,并查看是否有从另一个API收到的任何可能返回null的对象实例。