androidentity什么用_Android将String转换为StringEntity时的值不正确

我正在使用HttpPost向服务器发送一个字符串 . 这个字符串是一个JSONObject,我之前已经转换为String到目前为止一切正常...它必须与内容类型“application / json”一起发送 .

要发送它,将我的String转换为StringEntity并添加到httpPost.setEntity(my_string)...问题是服务器告诉我无法识别数据(已准备好接收转换为String的JSONObject)

在我的代码中,我在转换为StringEntity之前使用日志来了解String的值,结果如下:

String: {"Gender":"male","User":"Pedro","Email":"ejemplo@ejemplo.com","Language":"English"}

但是,当我将此String转换为StringEntity时,Log的结果,而不是相同,它如下所示:

String: org.apache.http.entity.StringEntity@16a4bc74

为什么?

我不知道我做错了什么......

我搜索了一下,发现了很多例子,我想我正确地做了......我不明白这个错误 . 为什么字符串在转换为StringEntity时没有保持值?

这是我的代码 .

我非常感谢你的帮助 .

问候 .

public static JSONObject makeServiceCall(String url,JSONObject params){

try {

Log.i("dmode", "LLegan los valores:" + " " + params);

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpEntity httpEntity = null;

HttpResponse httpResponse = null;

HttpPost httpPost = new HttpPost(url);

if (params != null) {

String conversion = params.toString();

Log.i("dmode", "JSONBoject to String" + " " + conversion);

StringEntity se;

se = new StringEntity(conversion);

Log.e("dmode", "String to StringEntity" + " " + se);

// Set HTTP parameters

httpPost.setHeader("Accept", "application/json");

httpPost.setHeader("Content-type", "application/json");

httpPost.setEntity(se);

}

String response = null;

httpResponse = httpClient.execute(httpPost);

httpEntity = httpResponse.getEntity();

response = EntityUtils.toString(httpEntity);

try {

jObject = new JSONObject(response);

} catch (Exception e) {

}

Log.i("dmode", "Devolución" + jObject);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return jObject;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值