首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android错误字符串无法转换为JSONArray

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

Stack Overflow用户
提问于 2018-11-29 12:52:04
回答 2查看 436关注 0票数 1

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

这是我的主activity.java:

代码语言:javascript
复制
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响应:

日志猫:

日志猫:

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53532057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档