0

嗨,我使用 android hive 示例创建了文件,但我似乎遇到了以下我无法弄清楚的错误。我的 UI 工作正常,只要我点击登录按钮或注册按钮,它就会强制关闭请帮助?

04-13 14:41:31.911: D/dalvikvm(431): GC_FOR_MALLOC freed 3881 objects / 200672 bytes in 88ms
04-13 14:41:32.110: E/JSON(431): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
04-13 14:41:32.110: E/JSON(431): <html><head>
04-13 14:41:32.110: E/JSON(431): <title>404 Not Found</title>
04-13 14:41:32.110: E/JSON(431): </head><body>
04-13 14:41:32.110: E/JSON(431): <h1>Not Found</h1>
04-13 14:41:32.110: E/JSON(431): <p>The requested URL /android_login_api/ was not found on >     this server.</p>
04-13 14:41:32.110: E/JSON(431): </body></html>
04-13 14:41:32.120: E/JSON Parser(431): Error parsing data org.json.JSONException: Value <!>     DOCTYPE of type java.lang.String cannot be converted to JSONObject
04-13 14:41:32.131: D/AndroidRuntime(431): Shutting down VM
04-13 14:41:32.131: W/dalvikvm(431): threadid=1: thread exiting with uncaught exception >     >     (group=0x4001d800)
04-13 14:41:32.131: E/AndroidRuntime(431): FATAL EXCEPTION: main
04-13 14:41:32.131: E/AndroidRuntime(431): java.lang.NullPointerException
04-13 14:41:32.131: E/AndroidRuntime(431):    at com.screenssample.LoginActivity$1.onClick>     >     (LoginActivity.java:59)
04-13 14:41:32.131: E/AndroidRuntime(431):    at android.view.View.performClick(View.java:2408)
04-13 14:41:32.131: E/AndroidRuntime(431):    at android.view.View$PerformClick.run>     >     >     (View.java:8816)
04-13 14:41:32.131: E/AndroidRuntime(431):    at android.os.Handler.handleCallback>     >     >     (Handler.java:587)
04-13 14:41:32.131: E/AndroidRuntime(431):    at android.os.Handler.dispatchMessage>     >     >     (Handler.java:92)
04-13 14:41:32.131: E/AndroidRuntime(431):    at android.os.Looper.loop(Looper.java:123)
04-13 14:41:32.131: E/AndroidRuntime(431):    at android.app.ActivityThread.main>     >     >     (ActivityThread.java:4627)
04-13 14:41:32.131: E/AndroidRuntime(431):    at java.lang.reflect.Method.invokeNative(Native >     Method)
04-13 14:41:32.131: E/AndroidRuntime(431):    at java.lang.reflect.Method.invoke>     >     >     (Method.java:521)
04-13 14:41:32.131: E/AndroidRuntime(431):    at om.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-13 14:41:32.131: E/AndroidRuntime(431):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-13 14:41:32.131: E/AndroidRuntime(431):    at dalvik.system.NativeStart.main(Native Method)

LoginActivity 代码读取

包 com.screenssample;

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.screenssample.DatabaseHandler; 
import com.screenssample.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(); 
            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(); 
        } 
    }); 
} 

}

4

2 回答 2

0
 E/JSON Parser(431): Error parsing data org.json.JSONException: Value <!>     DOCTYPE of type java.lang.String cannot be converted to JSONObject

似乎 json.getJSONObject("user"); 返回 XML 不是 JSONObject。

于 2012-04-13T21:58:01.907 回答
0

你的 PHP 文件在哪里?

04-13 14:41:32.110: E/JSON(431):

在 > 此服务器上未找到请求的 URL /android_login_api/。

看起来您没有指向正确的位置,您需要将配置中的给定 IP 更改为您拥有文件的位置。

于 2014-03-11T20:42:48.037 回答