dragon5006 2017-12-27 06:37
浏览 66
已采纳

通过PHP在Android中注册API连接时出错

I try to connect Login and Registration form through PHP API, But I get the error and I don't understand how its will be resolve. I check the solution regarding this error but I don't understand it. Thank you in advance!

Error :-

W/System.err: org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONObject

Here is my code for Registration form (Login.java)

Login. java (This is the java file where for Registration)

public class Login extends Activity {
    EditText editname, editemail, editpassword, editmobile;
    Button btnRegister;
    private static final String TAG = "Login";
    private static final String URL_FOR_REGISTRATION = "http://codexpertise.com/codexpertise.com/apitest/signup.php";
    ProgressDialog progressDialog;
    ImageButton btnfb;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        // Progress dialog
        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);

        editname = (EditText) findViewById(R.id.editname);
        editemail = (EditText) findViewById(R.id.editemail);
        editpassword = (EditText) findViewById(R.id.editpassword);
        editmobile = (EditText) findViewById(R.id.editmobile);
        btnRegister = (Button) findViewById(R.id.btnRegister);
        btnfb = (ImageButton)findViewById(R.id.btnfb);




        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitForm();
            }

            private void submitForm() {
                registerUser(editname.getText().toString(),
                        editemail.getText().toString(),
                        editpassword.getText().toString(),
                        editmobile.getText().toString());

            }

            private void registerUser(final String name,  final String email, final String password,
                                      final String mobile) {
                // Tag used to cancel the request
                String cancel_req_tag = "register";

                progressDialog.setMessage("Adding you ...");
                showDialog();

                StringRequest strReq = new StringRequest(Request.Method.POST,
                        URL_FOR_REGISTRATION, new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "Register Response: " + response.toString());
                        hideDialog();

                        try {
                            JSONObject jObj = new JSONObject(response);
                            boolean error = jObj.getBoolean("error");

                            if (!error) {
                                String user = jObj.getJSONObject("user").getString("name");
                                Toast.makeText(getApplicationContext(), "Hi " + user +", You are successfully Added!", Toast.LENGTH_SHORT).show();

                                // Launch login activity
                                Intent intent = new Intent(
                                        Login.this,
                                        MainActivity.class);
                                startActivity(intent);
                                finish();
                            } else {

                                String errorMsg = jObj.getString("error_msg");
                                Toast.makeText(getApplicationContext(),
                                        errorMsg, Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG, "Registration Error: " + error.getMessage());
                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_LONG).show();
                        hideDialog();
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() {
                        // Posting params to register url
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("name", name);
                        params.put("email", email);
                        params.put("password", password);
                        params.put("gender", mobile);
                        return params;
                    }
                };
                // Adding request to request queue
                AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);
            }

            private void showDialog() {
                if (!progressDialog.isShowing())
                    progressDialog.show();
            }

            private void hideDialog() {
                if (progressDialog.isShowing())
                    progressDialog.dismiss();
            }

        });

        btnfb.setOnClickListener(new View.OnClickListener() {
            @Override
              public void onClick(View view) {
                    Uri uri = Uri.parse("https://www.facebook.com/"); // missing 'http://' will cause crashed
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
            }
        });


    }

}

Here is the some image which i test it on postman

[enter image description here1

Check this

[enter image description here2

  • 写回答

1条回答 默认 最新

  • dsiy62758 2017-12-27 07:14
    关注

    Try this

    your response not any boolean error = jObj.getBoolean("error"); so you can read your response code and change your condition as your requirement..

     try 
        {
            JSONObject jObj = new JSONObject(response);
            String  respCode = jObj.getString("resp_code");
            //boolean error = jObj.getBoolean("error");
    
            if (respCode.equals("200")) {
                String user = jObj.getJSONObject("user").getString("name");
                Toast.makeText(getApplicationContext(), "Hi " + user +", You are successfully Added!", Toast.LENGTH_SHORT).show();
    
                // Launch login activity
                Intent intent = new Intent(
                        Login.this,
                        MainActivity.class);
                startActivity(intent);
                finish();
            } else {
    
                String errorMsg = jObj.getString("error_msg");
                Toast.makeText(getApplicationContext(),
                        errorMsg, Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。