2017-03-22 154 views
0

所以我試圖通過Xserve連接到我們的數據庫,當我試圖訪問令牌爲用戶。我正在使用正確的用戶名和密碼以及上下文類型和授予類型;我知道這是因爲我已經通過googles postmaster擴展嘗試了相同的POST方法。無論出於什麼原因,當我在Android上嘗試同樣的事情時,至少我認爲是相同的,它給了我一個400響應代碼,並且不返回任何內容。HttpURLConnection - 響應代碼:400(錯誤的請求)Android Studio - > xserve

下面是用於連接的代碼:我還沒有存儲結果到的JSONObject但我會稍後使用

private HttpURLConnection urlConnection; 

@Override 
    protected Boolean doInBackground(Void... params) { 
     Boolean blnResult = false; 
     StringBuilder result = new StringBuilder(); 
     JSONObject passing = new JSONObject(); 

     try { 
      URL url = new URL("http://xserve.uopnet.plymouth.ac.uk/modules/INTPROJ/PRCS251M/token"); 

      // set up connection 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setDoInput(true); 
      urlConnection.setDoOutput(true); 
      urlConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.connect(); 

      // set up parameters to pass 
      passing.put("username", mEmail); 
      passing.put("password", mPassword); 
      passing.put("grant_type", "password"); 

      // add parameters to connection 
      OutputStreamWriter wr= new OutputStreamWriter(urlConnection.getOutputStream()); 
      wr.write(passing.toString()); 


      // If request was good 
      if (urlConnection.getResponseCode() == 200) { 
       blnResult = true; 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(urlConnection.getInputStream())); 
       String line; 

       while ((line = reader.readLine()) != null) { 
        result.append(line); 
       } 
       reader.close(); 
      } 

      //JSONObject json = new JSONObject(builder.toString()); 
      Log.v("Response Code", String.format("%d", urlConnection.getResponseCode())); 
      Log.v("Returned String", result.toString()); 

     }catch(Exception e) { 
      e.printStackTrace(); 
     } 
     finally { 
      urlConnection.disconnect(); 
     } 

     return blnResult; 
    } 

,但我預計一些類型的輸出通過「Log.v 」。

有什麼突出的?

+0

請分享您的郵遞員請求和響應 – Ajinkya

+0

grant_type =密碼 – Zachary

+0

對評論的抱歉我試圖返回馬車,這就是我想要在Android重新創建 – Zachary

回答

1
try { 
     URL url = new URL("http://xserve.uopnet.plymouth.ac.uk/modules/INTPROJ/PRCS251M/token"); 

     parameters = new HashMap<>(); 
     parameters.put("username", mEmail); 
     parameters.put("password", mPassword); 
     parameters.put("grant_type", "password"); 

     set = parameters.entrySet(); 
     i = set.iterator(); 
     postData = new StringBuilder(); 

     for (Map.Entry<String, String> param : parameters.entrySet()) { 
      if (postData.length() != 0) { 
       postData.append('&'); 
      } 

      postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); 
      postData.append('='); 
      postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); 
     } 

     postDataBytes = postData.toString().getBytes("UTF-8"); 

     // set up connection 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setDoInput(true); 
     urlConnection.setDoOutput(true); 
     urlConnection.setConnectTimeout(5000); 
     urlConnection.setReadTimeout(5000); 
     urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.getOutputStream().write(postDataBytes); 

     // If request was good 
     if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
      reader = new BufferedReader(
        new InputStreamReader(urlConnection.getInputStream())); 
      String line; 

      while ((line = reader.readLine()) != null) { 
       result.append(line); 
      } 
      reader.close(); 
     } 

     Log.v("Login Response Code", String.valueOf(urlConnection.getResponseCode())); 
     Log.v("Login Response Message", String.valueOf(urlConnection.getResponseMessage())); 
     Log.v("Login Returned String", result.toString()); 


     jsonObject = new JSONObject(result.toString()); 
     token = jsonObject.getString("access_token"); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     urlConnection.disconnect(); 
     if (token != null) { 
      jsonObject = driverInfo(token); 

     } 
    } 

這個工程,雖然我已經把它移到它自己的功能了。 更改輸入類型爲HashMap

相關問題