Android错误String无法转换为JSONArray

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (87)

这是我的主要活动。我无法登录,因为它说java.lang.String无法转换为JSONArray。可能是什么错误?我无法解决它。

这是我的主要活动.java:

ppublic class MainActivity extends AppCompatActivity {
private EditText email, password;
private Button btn_login;
private ProgressBar loading;
private static String URL_LOGIN ="http://itsolutionsnepal.com/attendance/api/employeeLogin";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loading= findViewById(R.id.loading);
    email = findViewById(R.id.editTextUserEmail);
    password = findViewById(R.id.editTextPassword);
    btn_login = findViewById(R.id.buttonRegister);


    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String mEmail = email.getText().toString().trim();
            String mPassword = password.getText().toString().trim();

            if (!mEmail.isEmpty() || !mPassword.isEmpty()) {
                Login(mEmail, mPassword);
            }else{
                email.setError("Please Enter Email");
                password.setError("Please Enter Password");

            }


        }
    });

}

private void Login(final String email, final String password) {
    loading.setVisibility(View.VISIBLE);
    btn_login.setVisibility(View.GONE);

    StringRequest stringRequest = new StringRequest(Request.Method.POST,URL_LOGIN,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONArray jsonArray = new JSONArray(response);

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject object = jsonArray.getJSONObject(i);
                            String name = object.getString("_first_name").trim();
                            String email =  object.getString("_email").trim();

                            //  JSONObject jsnobject = new JSONObject(response);
//                            JSONArray jsonArray = jsnobject.getJSONArray("");
                           //JSONObject object = jsonArray.getJSONObject(i);


//                            JSONObject jsonResponse = new JSONObject(response);
//
//                            JSONArray jsonArray = jsonResponse.getJSONArray("");

                        //JSONObject jsonObject = new JSONObject(response);
                       // Log.d("JSON", jsonObject.toString());
                        //String success = jsonObject.getString("");
                       // JSONArray jsonArray = jsonObject.getJSONArray("");
                     //   JSONArray jsonArray = new JSONArray(response);
                       // JSONObject jsonObject = new JSONObject("");
                     //   String code = jsonObject.getString("");

  //                            if (success.equals("1"))
   //                            {

  //                                    String name = 
 object.getString("_first_name").trim();
//                                    String email = 
  object.getString("_email").trim();


 //                                    Toast.makeText(MainActivity.this, 
 "Success 
 Login \nYour name: "+name+"\nYour Email: "+email,
 //                                            Toast.LENGTH_LONG).show();
                            System.out.println("Value of response - " + 
response);

                            System.out.println("First Name : " + name);
                            System.out.println("Email : " + email);
                                Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                                intent.putExtra("name",name);
                                intent.putExtra("email", email);
                                startActivity(intent);
                            loading.setVisibility(View.GONE);




                            //}

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                        loading.setVisibility(View.GONE);
                        btn_login.setVisibility(View.VISIBLE);
                        Toast.makeText(MainActivity.this, "Error" +e.toString(),
                                Toast.LENGTH_SHORT).show();

                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, "Error" +error.toString(),
                            Toast.LENGTH_LONG).show();
                    loading.setVisibility(View.GONE);
                    btn_login.setVisibility(View.VISIBLE);

                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("_email", email);
            params.put("_password", password);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

这是我的json回复:

原木猫:

原木猫:

提问于
用户回答回答于

我认为你的请求没有标题,这就是你可以得到这个错误的原因。

@Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                //headers.put("Content-Type", "application/json");
                headers.put("key", "Value");
                return headers;
            }
用户回答回答于

做这个 :

private void Login(final String email, final String password) {
    loading.setVisibility(View.VISIBLE);
    btn_login.setVisibility(View.GONE);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, 
  URL_LOGIN,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                loading.setVisibility(View.GONE);
                    try {

                        JSONArray jsonArray = new JSONArray(response);
                        for (int i = 0; i<jsonArray.length(); i++){
                        JSONObject object = jsonArray.getJSONObject(i);

                        String name = object.getString("_first_name").trim();
                        String email =  object.getString("_email").trim();
                        System.out.println("First Name : " + name);
                        System.out.println("Email : " + email);
                        Intent intent = new Intent(MainActivity.this,HomeActivity.class);
                        intent.putExtra("name",name);
                        intent.putExtra("email", email);
                        startActivity(intent);
                        loading.setVisibility(View.GONE);



                            //}

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        loading.setVisibility(View.GONE);
                        btn_login.setVisibility(View.VISIBLE);
                        Toast.makeText(MainActivity.this, "Error" 
   +e.toString(),
                                Toast.LENGTH_SHORT).show();

                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, "Error" 
   +error.toString(),
                            Toast.LENGTH_LONG).show();
                    loading.setVisibility(View.GONE);
                    btn_login.setVisibility(View.VISIBLE);

                }
            })
    {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("_email", email);
            params.put("_password", password);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}
}

扫码关注云+社区

领取腾讯云代金券