2014-10-06 100 views
-5

我有這個JSON數據,我想使用httpurl連接發佈。使用HttpUrlConnection POST joson數據

POST http://example.com/2_1/payment/tel%3A%2B919825010000/transactions/amount HTTP/1.1 
Accept: application/json 
Content-Type: application/json 
X-Forwarded-For: 129.78.138.66, 129.78.64.103 
Authorization: Basic<base64 encoded application credentials> 
{ 
"amountTransaction": 
    { 
     "clientCorrelator": "54321", 
     "endUserId": "tel:+919825010000", 
     "paymentAmount": 
     { 
      "chargingInformation": 
       { 
        "code": "WGAME_0010_Ind00001111", 
        "description": ["Alien Invaders Game"] 
       }, 
      "chargingMetaData" : 
       { 
        "onBehalfOf" : "Example Games Inc", 
        "purchaseCategoryCode" : "Game", 
        "channel" : "SMS", 
        "taxAmount" : 0 
       } 
     }, 
     "referenceCode": "REF-12345", 
     "transactionOperationStatus": "Charged" 
     } 
    } 
} 

如何使用Httpurlconnection使所有這些數據的POST請求。?

+0

您是否將JSON對象添加爲標題字段? – erad 2014-10-06 19:18:11

回答

0

下面是一些你可以開始:

httpClient = createHttpClient(); 

//You wanna use POST method. 
mPost = new HttpPost(_urlStr); 

//Head 
mPost.addHeader(new BasicHeader("Content-Type", "application/json")); 

//Body 
((HttpPost) mPost).setEntity(new StringEntity(jsonText)); 

//Do it. 
client.execute(mPost); 

您還可以檢查出this tutorialthis tutorial更多的幫助。

相關問題