使用Json Object执行bufferreader时出错

时间:2013-02-05 17:43:44

标签: android json nullpointerexception bufferedreader

我的项目出错,似乎来自我的代码部分

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 Error(527):转换结果java.lang.NullPointerException时出错 02-05 17:46:27.286:E / JSON Parser(527):解析数据时出错org.json.JSONException:字符0处的输入结束 02-05 17:46:27.286:E / AndroidRuntime(527):致命异常:主要 02-05 17:46:27.286:E / AndroidRuntime(527):java.lang.NullPointerException 02-05 17:46:27.286:E / AndroidRuntime(527):at 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(Handler.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):at dalvik.system.NativeStart.main(Native Method)

这是我的loginacticvity.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();
    }

1 个答案:

答案 0 :(得分:0)

ADD: if(json!= null) 之前:  if(json.getString(KEY_SUCCESS)!= null)