开发者

how to send a json object from java to a .net client? [duplicate]

开发者 https://www.devze.com 2023-01-22 06:48 出处:网络
This question already has answers here: how to send a json string to a .NET REST service from java? (3 answers)
This question already has answers here: how to send a json string to a .NET REST service from java? (3 answers) Closed 8 years ago.

from my android client i am send开发者_StackOverflowing a json to string object.but the .net client is getting it as empty string. here is my code.

HttpPost request = new HttpPost(SERVICE_URI+"/save");
JSONStringer json = new JSONStringer()
.object()   
.key("cno").value("2000")
.key("cname").value("HI")
.key("cmail").value("HI")
.key("cphno").value("9292")
.key("cmailtype").value("Home")
.key("cphnotype").value("Office")
.key("clientno").value("1")
.endObject();
 String a= json.toString();
StringEntity entity = new StringEntity(a);
entity.setContentType("text/plain;charset=UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"text/plain;charset=UTF-8"));
request.setEntity(entity); 
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request); 

is this header format correct?


in my project i will use this

public JSONObject post(String url, JSONObject json) throws ClientProtocolException, IOException, JSONException, ServerResponseException{
    HttpPost httppost = new HttpPost(url);
    httppost.addHeader("Content-type","application/json");
    StringEntity se = new StringEntity(json.toString());
    se.setContentType("text/xml");
    se.setContentEncoding( new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    httppost.setEntity(se);
    HttpResponse response = client.execute(httppost);
    HttpEntity entity = response.getEntity();
    if(entity != null&&(response.getStatusLine().getStatusCode()==201||response.getStatusLine().getStatusCode()==200)){
        //your function on output

}
    else
        throw new ServerResponseException(response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase());

}

the output of this methtod is JSONObject because my output is json

0

精彩评论

暂无评论...
验证码 换一张
取 消