Http Post With Body

前端 未结 5 407
轻奢々
轻奢々 2020-12-13 15:36

i have sent method in objective-c of sending http post and in the body i put a string:

NSString *requestBody = [NSString stringWithFormat:@\"mystring\"];
NSM         


        
5条回答
  •  醉酒成梦
    2020-12-13 16:24

    You can use HttpClient and HttpPost to send a json string as body:

    public void post(String completeUrl, String body) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(completeUrl);
        httpPost.setHeader("Content-type", "application/json");
        try {
            StringEntity stringEntity = new StringEntity(body);
            httpPost.getRequestLine();
            httpPost.setEntity(stringEntity);
    
            httpClient.execute(httpPost);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    

    Json body example:

    {
      "param1": "value 1",
      "param2": 123,
      "testStudentArray": [
        {
          "name": "Test Name 1",
          "gpa": 3.5
        },
        {
          "name": "Test Name 2",
          "gpa": 3.8
        }
      ]
    }
    

提交回复
热议问题