使用HttpUrlConnection POST POST json数据

时间:2014-10-06 19:04:07

标签: java android json post httpurlconnection

我有这个JSON数据,我想使用httpurlconnection发布。

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请求。?

1 个答案:

答案 0 :(得分: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以获取更多帮助。