2012-03-07 82 views
0

因此,使用OAuth API我需要檢索access_token屬性。發送登錄信息工作正常,我確實得到一個適當的迴應。響應的格式爲這樣:Android httpreponse(urlencoded字符串)獲取屬性

access_token=SOMETOKEN&login=SOMELOGIN&apiKey=SOMEAPIKEY 

目前我得到的迴應是這樣的:

org.apache.http.HttpResponse response = httpclient.execute(httppost); 
String content = convertStreamToString(response.getEntity().getContent()); 

其中所描述的第二行獲取內容的字符串。所以返回的是一個URL編碼字符串,我需要獲取它的access_token值。然而,我不知道如何做到這一點,我似乎無法找到一種方法或任何這樣做,我必須解析它或有其他一些方法來做到這一點嗎?

任何幫助,非常感謝。

回答

0

我其實只是發現這個位於Twitter的一個解決方案API我(http://kenai.com/projects/twitterapime/pages/Home)StringUtil類,我不會在這裏貼,但它的開放魅力,所以如果任何人需要它,去那裏得到它。

0

嘗試URLDecoder.decode(content)

如果這是唯一的問題,應該工作。

編輯:我想你已經知道這一點,你正在尋找的東西更加自動化,但以防萬一。我沒有測試過它。

String[] splitted = content.split("&"); 

//detect token 
String token; 
for(int i=0;i<splitted.length;i++){ 
if(splitted[i].startsWith("token"){ 
String[] tokenSplitted = splitted[i].split("="); 
token = tokenSplitted[1]; 
} 
} 
+0

不,我不需要解碼它,我需要得到一個字符串的具體參數,可能看起來像︰access_token = n6hfcq3iio&login = username&apiKey = duksqmo98f所以我需要提取的是n6hfcq3iio – AzaraT 2012-03-07 17:20:35

+0

檢查我的答案 – Maragues 2012-03-07 18:16:04