在android中获取内存错误

时间:2013-04-02 07:46:41

标签: android json

在我的应用程序中,我可以通过使用json向本地服务器发送请求来登录。我正在使用共享首选项保存json的所有数据。点击退出按钮后,它将返回登录活动并删除共享首选项中的所有数据。但是在登录后我将使用任何其他email_id,密码登录时,它会显示以下错误:

04-02 13:02:25.161: E/AndroidRuntime(1061): FATAL EXCEPTION: main
04-02 13:02:25.161: E/AndroidRuntime(1061): java.lang.OutOfMemoryError
04-02 13:02:25.161: E/AndroidRuntime(1061):     at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at java.lang.StringBuilder.append(StringBuilder.java:271)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at java.io.BufferedReader.readLine(BufferedReader.java:417)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at com.example.androidhive.library.JSONParser.getJSONFromUrl(JSONParser.java:59)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at com.example.androidhive.library.UserFunctions.loginUser(UserFunctions.java:37)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at com.example.androidhive.LoginActivity$1.onClick(LoginActivity.java:93)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at android.view.View.performClick(View.java:4084)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at android.view.View$PerformClick.run(View.java:16966)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at android.os.Handler.handleCallback(Handler.java:615)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at android.os.Looper.loop(Looper.java:137)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at java.lang.reflect.Method.invokeNative(Native Method)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at java.lang.reflect.Method.invoke(Method.java:511)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-02 13:02:25.161: E/AndroidRuntime(1061):     at dalvik.system.NativeStart.main(Native Method)

我正在使用这段代码登录:

 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");
            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");

                        SharedPreferences prefs = getSharedPreferences(share_pref_file, Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.putString("jsondata", json.toString());
                        editor.commit();

                        // Clear all previous data in database
                        // userFunction.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_EMAIL),
                                json.getString(KEY_UID));
                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(
                                getApplicationContext(),
                                DashboardActivity.class);
                        dashboard.putExtra("email", email);
                        dashboard.putExtra("password", password);
                        // 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();
            }

这里是JSONParser.java:59的代码:

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

第二个错误代码行是:

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

那么你能告诉我第二次登录失败的原因是什么吗?提前谢谢。

0 个答案:

没有答案