Android:Http文章,参数不工作

Android:Http文章,参数不工作,第1张

概述我需要创建一个包含参数的HTTP POST请求.我知道有很多例子,我尝试使用HTTPparams,NameValuePair等,但似乎无法获得正确的服务器格式. 服务器类型:基于REST的API,使用JSON进行数据传输 Content-type:application / json 接受:application / json 内容长度:47 { “用户名”: “ABCD”, “密码”: “123 我需要创建一个包含参数的http POST请求.我知道有很多例子,我尝试使用httpparams,nameValuePair等,但似乎无法获得正确的服务器格式.

服务器类型:基于REST的API,使用JSON进行数据传输
Content-type:application / Json
接受:application / Json
内容长度:47
{ “用户名”: “ABCD”,“密码”: “1234”}

我可以传递这些标题,但我似乎无法通过这些参数“用户名”,“密码”.这是我的代码:

httpClIEnt clIEnt = new DefaulthttpClIEnt();      httpPost post = new httpPost("http://www.mymi5.net/API/auth/login");       List<nameValuePair> pairs = new ArrayList<nameValuePair>();      pairs.add(new BasicnameValuePair("username","abcd"));      pairs.add(new BasicnameValuePair("password","1234"));      post.setheader("Content-type","application/Json");    post.setheader("Accept","application/Json");    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,"UTF-8");      post.setEntity(entity);      httpResponse response = clIEnt.execute(post);

我试图调试,但不能看到实体是否正确连接…我做错了什么?

提前致谢.
玛斯

解决方法 尝试这个:
httpClIEnt clIEnt = new DefaulthttpClIEnt();      httpPost post = new httpPost("http://www.mymi5.net/API/auth/login");       post.setheader("Content-type","application/Json");JsONObject obj = new JsONObject();obj.put("username","abcd");obj.put("password","1234");    post.setEntity(new StringEntity(obj.toString(),"UTF-8"));    httpResponse response = clIEnt.execute(post);
总结

以上是内存溢出为你收集整理的Android:Http文章,参数不工作全部内容,希望文章能够帮你解决Android:Http文章,参数不工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1133643.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存