2013-07-16 22 views
0

形式我有這個字符串(postInfo):如何通過一個字符串HttpPost將它作爲Android中

"Content-Type=application/x-www-form-urlencoded&grant_type=refresh_token 
&refresh_token=1/bNKLGjsbgYwkNytEwpNhgfTyuDs34Klkjtqp2AZKnqs& 
client_secret=mySecret&client_id=my_id.apps.googleusercontent.com" 

我需要能夠將其發送到HttpPost,以儘自己的作品,等等。我一直在Android中測試它,但除非它是一個關鍵值對,否則它將不起作用。我只是解析它,如果字符串將永遠是相同的,但我不能保證。在測試時,我意識到爲什麼它沒有工作。它不作爲請求正文中的一個表格。有趣的是,在iOs中它確實沒有太多的工作。這是我的java:

public String post(String leUri, String data) { 
     /*=https://accounts.google.com/o/oauth2/token*/ 
     HttpPost httpPost = new HttpPost(leUri); 
      try {    
      httpPost.setEntity(new StringEntity(postInfo)); 
     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      return "An Error Ocurred"; 
     } 
      try { 
      HttpResponse response = httpClient.execute(httpPost); 
      HttpEntity entity = response.getEntity(); 
     /*...*/ 

它不會以表格形式出現。我怎樣才能做到這一點?我肯定錯過了什麼。謝謝。

回答

0

你必須拆分你的postInfo,如下所示,並檢查編碼網站正在採取的職位。通常它是UTF-8

//設置內容類型

httpPost.setHeader( 「內容類型」, 「應用程序/ X WWW的窗體-urlencoded;字符集= UTF-8」);

//添加您的數據

httpPost.setEntity(新UrlEncodedFormEntity( 「grant_type = refresh_token & refresh_token = 1/bNKLGjsbgYwkNytEwpNhgfTyuDs34Klkjtqp2AZKnqs & client_secret = mySecret &的client_id = my_id.apps.googleusercontent.com」,「UTF -8" ));

相關問題