0

我的项目有错误,它似乎来自我的代码的这一部分

try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());

在此之后,我将字符串解析为 jsonobject

有人可以帮忙吗?

这是我的 logcat “错误”文件

02-05 17:46:27.286:E/Buffer 错误(527):错误转换结果 java.lang.NullPointerException 02-05 17:46:27.286:E/JSON 解析器(527):解析数据时出错 org.json.JSONException : 02-05 17:46:27.286 字符 0 处的输入结束:E/AndroidRuntime(527): FATAL EXCEPTION: main 02-05 17:46:27.286: E/AndroidRuntime(527): java.lang.NullPointerException 02 -05 17:46:27.286: E/AndroidRuntime(527): 在 com.example.androidhive.LoginActivity$1.onClick(LoginActivity.java:65) 02-05 17:46:27.286: E/AndroidRuntime(527): 在android.view.View.performClick(View.java:2408) 02-05 17:46:27.286: E/AndroidRuntime(527): 在 android.view.View$PerformClick.run(View.java:8816) 02-05 17:46:27.286: E/AndroidRuntime(527): 在 android.os.Handler.handleCallback(Handler.java:587) 02-05 17:46:27.286: E/AndroidRuntime(527): 在 android.os.Handler .dispatchMessage(处理程序.java:92) 02-05 17:46:27.286: E/AndroidRuntime(527): 在 android.os.Looper.loop(Looper.java:123) 02-05 17:46:27.286: E/AndroidRuntime(527): 在android.app.ActivityThread.main(ActivityThread.java:4627) 02-05 17:46:27.286: E/AndroidRuntime(527): at java.lang.reflect.Method.invokeNative(Native Method) 02-05 17:46 :27.286: E/AndroidRuntime(527): at java.lang.reflect.Method.invoke(Method.java:521) 02-05 17:46:27.286: E/AndroidRuntime(527): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-05 17:46:27.286: E/AndroidRuntime(527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02 -05 17:46:27.286:E/AndroidRuntime(527):在 dalvik.system.NativeStart.main(本机方法)app.ActivityThread.main(ActivityThread.java:4627) 02-05 17:46:27.286: E/AndroidRuntime(527): 在 java.lang.reflect.Method.invokeNative(Native Method) 02-05 17:46:27.286 : E/AndroidRuntime(527): at java.lang.reflect.Method.invoke(Method.java:521) 02-05 17:46:27.286: E/AndroidRuntime(527): at com.android.internal.os。 ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-05 17:46:27.286: E/AndroidRuntime(527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-05 17:46:27.286:E/AndroidRuntime(527):在 dalvik.system.NativeStart.main(本机方法)app.ActivityThread.main(ActivityThread.java:4627) 02-05 17:46:27.286: E/AndroidRuntime(527): 在 java.lang.reflect.Method.invokeNative(Native Method) 02-05 17:46:27.286 : E/AndroidRuntime(527): at java.lang.reflect.Method.invoke(Method.java:521) 02-05 17:46:27.286: E/AndroidRuntime(527): at com.android.internal.os。 ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-05 17:46:27.286: E/AndroidRuntime(527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-05 17:46:27.286:E/AndroidRuntime(527):在 dalvik.system.NativeStart.main(本机方法)在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-05 17:46:27.286: E/AndroidRuntime(527): 在 com.android.internal.os.ZygoteInit.main( ZygoteInit.java:626) 02-05 17:46:27.286: E/AndroidRuntime(527): at dalvik.system.NativeStart.main(Native Method)在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-05 17:46:27.286: E/AndroidRuntime(527): 在 com.android.internal.os.ZygoteInit.main( ZygoteInit.java:626) 02-05 17:46:27.286: E/AndroidRuntime(527): at dalvik.system.NativeStart.main(Native Method)

这是我的 loginactivity.java

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();
        }
    });
}

}

这是 loginuser 方法

    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.d("JSON", "issa");
    return json;
}

这是 gestjsonfromurl

 public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
4

1 回答 1

0

添加:如果(json!= null)之前:如果(json.getString(KEY_SUCCESS)!= null)

于 2013-05-09T14:57:06.353 回答