2017-02-16 128 views
1

我使用Volley庫和意外的響應代碼400.我試圖從用戶名和密碼登錄,但面臨com.android.volley.ServerError.How可以解決這個問題嗎?意外的響應代碼400?

登錄類

public class Login extends AppCompatActivity implements View.OnClickListener { 

    EditText userName, Password; 
    Button login; 
    public static final String LOGIN_URL = "http://192.168.0.106:84/Token"; 
    public static final String KEY_USERNAME = "UserName"; 
    public static final String KEY_PASSWORD = "Password"; 
    String username, password; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     userName = (EditText) findViewById(R.id.login_name); 
     Password = (EditText) findViewById(R.id.login_password); 
     userName.setHint(Html.fromHtml("<font color='#008b8b' style='italic'>Username</font>")); 
     Password.setHint(Html.fromHtml("<font color='#008b8b'>Password</font>")); 
     login = (Button) findViewById(R.id.login); 
     login.setOnClickListener(this); 
    } 

    private void UserLogin() { 

     username = userName.getText().toString().trim(); 
     password = Password.getText().toString().trim(); 
     StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.POST, LOGIN_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         if (response.trim().equals("success")) { 
          openProfile(); 

         } else { 
          Toast.makeText(Login.this, response, Toast.LENGTH_LONG).show(); 
         } 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

         Toast.makeText(Login.this, error.toString(), Toast.LENGTH_LONG).show(); 
        } 
       }) { 


      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       HashMap<String, String> params = new HashMap<String, String>(); 
       params.put("Content-Type", "application/x-www-form-urlencoded"); 
       params.put("Charset", "UTF-8"); 
       return params; 
      } 


      @Override 
      public Map<String, String> getParams() throws AuthFailureError { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put(KEY_USERNAME, username); 
       map.put(KEY_PASSWORD, password); 
       return map; 
      } 
     }; 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 
    } 


    private void openProfile() { 
     Intent intent = new Intent(this, Home.class); 
     intent.putExtra(KEY_USERNAME, username); 
     startActivity(intent); 
    } 

    @Override 
    public void onClick(View v) { 

     UserLogin(); 
    } 


    public String getBodyContentType() { 
     return "application/xml"; 
    } 
} 

如何這個問題可以解決?

刪除標題後,我得到了這個。 [![在這裏輸入的形象描述] [1] [1]

+0

無從得知沒有看到服務器的詳細信息錯誤。儘管如此,您仍將頭添加到參數中,這肯定是錯誤的。 –

+0

錯誤代碼400意味着錯誤的請求。檢查你的服務器端需要什麼類型的數據。 – Piyush

+0

@Robby getHeaders()方法也會給出相同的錯誤。 – seon

回答

0

試試這個代碼 這種使用POST方法

final String URL = Vcruit.getInstance().GET_LOGIN; 
    // Post params to be sent to the server 
    HashMap<String, String> params = new HashMap<String, String>(); 
    params.put("apikey", xxx.getInstance().API_KEY); 
    params.put("userName", Username); 
    params.put("password", Password); 


     JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params), 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        try { 
         VolleyLog.v("Response:%n %s", response.toString(4)); 
         JSONObject jObj = new JSONObject(response.toString()); 
         JSONArray results = jObj.getJSONArray("data"); 
         for (int i = 0; i < results.length(); i++) { 
          JSONObject cat = results.getJSONObject(i); 
          String status = cat.getString("status"); 
          String errormsg = cat.getString("ErrorMsg"); 

          if (status.equals("Success")) { 
                   } else { 


          } 
          } 

        } catch (JSONException e) { 

         e.printStackTrace(); 
        } 
        loader.setVisibility(View.GONE); 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.e("Error: ", error.getMessage()); 
     } 
    }); 

    xxx.getInstance().addToRequestQueue(req); 
} 

https://www.simplifiedcoding.net/android-studio-volley-tutorial-to-create-a-login-application/

+0

提供給一個網址例如 – seon

+0

看看編輯。@斤你已經登錄教程嗎?它會幫助我,如果你寄。 –

+0

我必須先登錄不註冊任何有用links.please – seon

相關問題