2017-06-16 54 views
1

我正在嘗試使用排球庫登錄。我使用SharedPrefrences來存儲用戶名,電子郵件,手機。當我使用正確的手機號碼時。和密碼。 Toast生成登錄成功,但無法將Login Fragment移動到儀表板活動。無法在Volley Library中使用Intent從片段中移動下一個活動

這裏是登錄方法的代碼

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響應

{ 「成功」: 「真」, 「消息」: 「登錄成功地」, 「用戶」:[{ 「名」: 「satishkanaujiya」, 「電子郵件」: 「***** @ gmail.com」, 「手機」: 「901589 ****」}]}

+0

你應該getActivity()。startActivity(意向) –

+0

@Sandeepdhiman我試過,但沒有幫助做到這一點。 – Codelover

回答

0

這是一個數組,我將它視爲對象。最後,我通過以下代碼糾正了我的錯誤。

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

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

OR

使用LocalBraodcastManager: 使用此鏈接:how to use LocalBroadcastManager?