Android错误字符串无法转换为JSONArray

时间:2018-11-29 04:52:04

标签: php android android-studio

这是我的主要活动。我无法登录,因为它说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响应:

enter image description here

日志猫:

enter image description here

日志猫:

enter image description here

2 个答案:

答案 0 :(得分:0)

执行此操作:

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

}
}

答案 1 :(得分:0)

我认为您没有请求的标头,因此您会收到此错误。

@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;
            }