2010-10-29 11 views
0

à partir de mon client android J'envoie un json à l'objet chaîne. Mais le client .net l'obtient comme une chaîne vide. voici mon code.comment envoyer un objet json de java à un client .net?

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

Ce format d'en-tête est-il correct?

Répondre

2

dans mon projet, je vais utiliser ce

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

} 

la sortie de ce methtod est JSONObject parce que ma sortie est JSON

+0

quand j'utilise ce code je reçois Mauvaise Demande. J'utilise aussi le code similaire mais dono pourquoi son erreur, ou u en utilisant le service .net? – Ads

+0

essayez également avec StringEntity se = new StringEntity (JSON: + json.toString()); – DX89B

+0

s je le faisais aussi mais je ne suis pas sûr y son reposnse est mauvaise demande – Ads