如何修复null对象引用上的java.lang.String org.json.JSONObject.toString()'?

时间:2015-08-09 15:33:48

标签: php android sql json login

我的android模拟器在尝试运行登录活动时,每当我尝试运行它时都会抛出一个空对象引用。

一旦到达以下行,它就会这样做:

 // check your log for json response
            Log.d("Login attempt", json.toString());

这是完整的login.java代码:

public class login extends Activity implements View.OnClickListener {
    private AnimatedGifImageView animatedGifImageView;
    public EditText user, pass;
    private Button mSubmit, mRegister;

    // Progress Dialog
    private ProgressDialog pDialog;


    JSONParser jsonParser = new JSONParser();


    //php login script location:

    //localhost :
    //testing on your device
    //put your local ip instead,  on windows, run CMD > ipconfig
    //or in mac's terminal type ifconfig and look for the ip under en0 or en1
    // private static final String LOGIN_URL = "http://xxx.xxx.x.x:1234/webservice/login.php";

    //testing on Emulator:
    private static final String LOGIN_URL = "http://www.samplephppage.com";

    //testing from a real server:
    //private static final String LOGIN_URL = "http://www.yourdomain.com/webservice/login.php";

    //JSON element ids from repsonse of php script:
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        user = (EditText)findViewById(R.id.etUsername);
        pass = (EditText)findViewById(R.id.etPassword);

        animatedGifImageView = ((AnimatedGifImageView) findViewById(R.id.animatedGifImageView));
        animatedGifImageView.setAnimatedGif(R.raw.animated_gif_big, TYPE.AS_IS);


        //setup input fields

        //setup buttons
        mSubmit = (Button) findViewById(R.id.btnLogin);

        //register listeners
        mSubmit.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btnLogin:
                new AttemptLogin().execute();
                break;
            case R.id.textView6:
                Intent i = new Intent(this, forgotpassword.class);
                startActivity(i);
                break;

            default:
                break;
        }
    }


    class AttemptLogin extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        boolean failure = false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(login.this);
            pDialog.setMessage("Checking Credentials...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;

            String etUsername = user.getText().toString();
            String etPassword = pass.getText().toString();


            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<>();
                params.add(new BasicNameValuePair("username", etUsername));
                params.add(new BasicNameValuePair("password", etPassword));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);

                // check your log for json response
                **Log.d("Login attempt", json.toString());** <--- **Crashes Here**

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("Login Successful!", json.toString());
                    Intent i = new Intent(login.this, LoginLoading.class);
                    finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                }else{
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }

这是php页面的代码

   <?php
if($count == 1){
    $response['success'] = 1;
    $response['message'] = "Login Successful"
    die(json_encode($response));
    }
    ?>

我似乎可以弄清楚为什么它会给我这个例外?

3 个答案:

答案 0 :(得分:2)

只需检查您的Web服务实现是否通过在任何浏览器中点击LOGIN_URL来为您提供JSON响应。 如果响应出现,那么也请尝试使用您的应用,否则请检查您的网络服务中的错误

答案 1 :(得分:1)

确保您的登录网址+您的参数具有逻辑意义。在您的代码中记录并在浏览器中执行发布请求,以查看请求是否实际返回您想要的JSON。

答案 2 :(得分:-1)

因为:

private static final String LOGIN_URL = "my php page";

不是有效的网址。

阅读上面那一行的评论,以获取有关应将LOGIN_URL分配给哪些人的建议。