2015-08-30 110 views
0

作爲Android開發者,我仍然是新手,特別是關於GET/POST數據與登錄腳本的Oauth 2.0功能。我不能使用過時的方法,如HttpClient的和HttpPost我的應用程序下面的API支持23:POST數據與HttpURLConnection

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(); 

我不得不使用HttpURLConnection的方法像this link計劃,但仍然不知道與下面的請求參數添加它放在我的腳本:

  • grant_type:字符串

  • CLIENT_ID:字符串

  • client_secret:串

  • 用戶名:字符串

  • 密碼:串

  • 的access_token:串

請求示例:

POST /的access_token HTTP/1.1

主機:api.example.com

緩存控制:無緩存

內容類型:應用/ X WWW的窗體-urlencoded

grant_type =密碼& CLIENT_ID = [client_od] & client_secret = [client_secret] &用戶名=名稱%40email.com &口令= 1234

全成登錄後我能得到響應例如像貝洛w:

"time": 0.2429, 
"environment": "DEVELOPMENT", 
"status": { 
"code": 200, 
"description": "OK" 
}, 
"data": { 
"access_token": "shddsldshldslkdsklksdldslkdslksdlkds", 
"token_type": "Bearer", 
"expires_in": 86400, 
"refresh_token": "sekhewihewiiewjejwojewojoewhik" 
} 
} 

我怎樣才能算出它,所以我可以得到像上面那樣的響應例子?

回答

1

我會建議使用循環J庫,給你RequestParamsAsyncHttpClient完成你的任務..

Get Jar and explanation from here

RequestParams params = new RequestParams(); 
    params.put("username", "james"); 
    params.put("password", "123456"); 
    params.put("email", "[email protected]"); 
    //------add params here-------// 

    AsyncHttpClient client = new AsyncHttpClient(); 
    client.post("IP/URL", params, new AsyncHttpResponseHandler() { 

     @Override 
     public void onSuccess(int statusCode, Header[] headers, byte[] response) { 
      try { 
       JSONObject json = new JSONObject(""); 
       //----- parse json 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) { 
      //---failure massage 
     } 
    }); 
相關問題