简体   繁体   English

通过在Volley Library中使用Intent,无法从片段移动下一个Activity

[英]Unable to move on next Activity from fragment by using Intent in Volley Library

I am trying to login by using volley library. 我正在尝试使用排球库登录。 I have used SharedPrefrences to store User name,email,mobile. 我使用SharedPrefrences来存储用户名,电子邮件,移动设备。 When I am using correct mobile no. 当我使用正确的手机没有。 and password. 和密码。 Toast is generating to login successful but not able to move Login Fragment to Dashboard Activity. Toast生成登录成功但无法将Login Fragment移动到Dashboard Activity。

Here is the Code of login method 这是登录方法的代码

private void login(String login_url, final String getLoginMob, final String getLoginPwd) {

    //Progress Dialog code
    final Dialog dialog =new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.loading_dialog);
    dialog.setCancelable(false);
    dialog.show();

    RequestQueue queue = Volley.newRequestQueue(getActivity());
    StringRequest postrequest = new StringRequest(Request.Method.POST, login_url, new Response.Listener<String>() {


        @Override
        public void onResponse(String response) {
            dialog.dismiss();

            try {
                JSONObject jsonObject = new JSONObject(response);

                if (jsonObject.getBoolean("success") == true ) {
                    Toast.makeText(getActivity(),jsonObject.getString("message"),Toast.LENGTH_LONG).show();

                    JSONObject jsonObjectInfo=jsonObject.getJSONObject("User");
                    sharedPrefrence_main.setName(jsonObjectInfo.getString("name"));
                    sharedPrefrence_main.setEmail(jsonObjectInfo.getString("email"));
                    sharedPrefrence_main.setMobile_no(jsonObjectInfo.getString("mobile"));

                    Intent intent=new Intent(getActivity(), Dashboard.class);
                    startActivity(intent);


                } else if (jsonObject.getBoolean("success") == false) {
                    Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
                }
                else
                    Toast.makeText(getActivity(),"Entries are wrong",Toast.LENGTH_LONG).show();


            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
            dialog.dismiss();
        }
    }){
        @Override
        protected Map<String, String> getParams(){
            Map<String,String> param=new HashMap<String, String>();
            param.put("mobile_email", getLoginMob);
            param.put("password", getLoginPwd);
            return param;

        }
    };

    int socketTimeout = 30000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    postrequest.setRetryPolicy(policy);
    queue.add(postrequest);

}

JSON Response JSON响应

{"success":"true","message":"Login Sucessfully","User":[{"name":"satishkanaujiya ","email":"*****@gmail.com","mobile":"901589****"}]} {“success”:“true”,“message”:“登录成功”,“用户”:[{“name”:“satishkanaujiya”,“email”:“***** @ gmail.com”,“mobile” “:” 901589 ****“}]}

  runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //Type your Intent code here
        }
    });

OR 要么

Use LocalBraodcastManager: Use this link: how to use LocalBroadcastManager? 使用LocalBraodcastManager:使用此链接: 如何使用LocalBroadcastManager?

This is an array and I was treating it as objects. 这是一个数组,我把它当作对象。 Finally I corrected my mistakes by following codes. 最后我通过遵循代码纠正了我的错误。

                JSONArray jsonArrays=jsonObject.getJSONArray("User");
                for(int i=0;i<jsonArrays.length();i++){
                 JSONObject jsonObject1=jsonArrays.getJSONObject(i);
                    sharedPrefrence_main.setName(jsonObject1.getString("name"));
                    sharedPrefrence_main.setEmail(jsonObject1.getString("email"));
                    sharedPrefrence_main.setMobile_no(jsonObject1.getString("mobile"));

                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM