代码之家  ›  专栏  ›  技术社区  ›  Ads

如何将json对象从java发送到.net客户端?[副本]

  •  0
  • Ads  · 技术社区  · 16 年前

    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); 
    

    这个标题格式正确吗?

    2 回复  |  直到 16 年前
        1
  •  2
  •   DX89B    16 年前

    在我的项目中我将使用这个

    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());
    
    }
    

    这个methtod的输出是JSONObject,因为我的输出是json

    推荐文章