Android的DefaultHttpClient POST到服务WCF的.svc [英] android DefaultHttpClient POST to service wcf .svc
问题描述
我哈瓦这个code
public static String methodPost(final String url, final String dataToPost) throws ClientProtocolException,
IOException, IllegalStateException, Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(dataToPost);
se.setContentEncoding("UTF-8");
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
return streamToString(httpEntity.getContent());
}
我在IPHONE发布此和完美的工作:
I post this in IPHONE and work perfectly:
@"{\"var1\":\"Value1\"}{\"var2\":[{\"item1\":\"1\"},{\"item1\":\"Value2\"}]}"
当我还张贴在Android系统。
when I post also in ANDROID.
Response return HTTP 1 400
例如:
methodPost(url, "{\"var1\":\"Value1\"}{\"var2\":[{\"item1\":\"1\"},{\"item1\":\"Value2\"}]}");
不过,我发布此
methodPost(url, "{\"var1\":\"Value1\"}{\"var2\":\"Value2\"}");
这项工作只完美,当我用[]我有一些误差
this work perfectly only when I use "[ ]" i have some error
对不起我的英语:)
推荐答案
科莫VEO阙ERES墨西哥TE puedo contestar西班牙文,不料却突intentas mandar人WCF儿子未CONJUNTO德CADENAS德的Json,Y迪登特鲁德ESE CONJUNTO埃尔problema ES阙本身encuentran listas德UN MISMO objecto,阙德HECHO本身重新presentarian算法中ASI科莫ESTO ....
Como veo que eres de mexico te puedo contestar en español, lo que tu intentas mandar al WCF son un conjunto de Cadenas de Json, y dentro de ese conjunto el problema es que se encuentran listas de un mismo objecto, que de hecho se representarian algo asi como esto....
{\\VAR1 \\:\\值1 \\} {\\VAR2 \\:[{\\ITEM1 \\:\\XX \\},{\\ITEM1 \\:\\YY \\ }]}
"{\"var1\":\"Value1\"}{\"var2\":[{\"item1\":\"XX\"},{\"item1\":\"YY\"}]}"
ESO,人mandarlo人WCF科莫联合国字符串没有卤味reconocerá,老阙阙tienes ES hacer巴刹ESE一个字符串变量UNA去DATOSŸ欧空局变量mandarla人WCF。 Quedaria算法中ASI ...
Eso, al mandarlo al WCF como un String no lo reconocerá, lo que tienes que hacer es pasar ese string a una variable de datos y esa variable mandarla al WCF . Quedaria algo asi...
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(dataToPost.getBytes());
byteArrayEntity.setContentEncoding("UTF-8");
byteArrayEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(byteArrayEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
return streamToString(httpEntity.getContent());
雅CON ESO,podras recibir土族DATOS连接埃尔WCF连接形式上德listas德objectos。
Ya con eso, podras recibir tus datos en el WCF en forma de listas de objectos.
这篇关于Android的DefaultHttpClient POST到服务WCF的.svc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!