Android Webview能否 post提交Json?

NASA_001 2015-07-05 03:11:16
网站的登录接口使用post方式提交 参数用Json格式
...全文
2941 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
NASA_001 2015-07-06
  • 打赏
  • 举报
回复
引用 5 楼 hjq2013 的回复:
[quote=引用 4 楼 NASA_001 的回复:]
[quote=引用 2 楼 hjq2013 的回复:]
//需要访问的网址
String url = "http://www.cqjg.gov.cn/netcar/FindThree.aspx";
//post访问需要提交的参数
String postDate = "txtName=zzz&QueryTypeLst=1&CertificateTxt=dsds";
//由于webView.postUrl(url, postData)中 postData类型为byte[] ,
//通过EncodingUtils.getBytes(data, charset)方法进行转换
webView.postUrl(url, EncodingUtils.getBytes(postDate, "BASE64"));


不行啊 我要请求的参数是json格式的 EncodingUtils.getBytes(postDate, "BASE64")会报错

“The method getBytes(String, String) in the type EncodingUtils is not applicable for the arguments (JSONObject, String)”[/quote]
public void postUrl(String url, byte[] postData) 这个是方法的原始签名
你将你的json 对象转成String 再转成byte就行了[/quote]

我的相关代码

JSONObject jsonObject = new JSONObject();
jsonObject.put("userCode", username);
jsonObject.put("pwd", userpwd);
jsonObject.put("orgCode", code);
jsonObject.put("clientType", type);

StringEntity se = null;
try {
se = new StringEntity(jsonObject.toString());
}
catch (UnsupportedEncodingException e)
{ // TODO Auto-generated catch block

e.printStackTrace();
}
byte[] result = null;
try {
result = EntityUtils.toByteArray(se);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

webView.postUrl(httpurl, result);


运行起来的结果还是出错
Hare_ 2015-07-06
  • 打赏
  • 举报
回复
引用 4 楼 NASA_001 的回复:
[quote=引用 2 楼 hjq2013 的回复:]
//需要访问的网址
String url = "http://www.cqjg.gov.cn/netcar/FindThree.aspx";
//post访问需要提交的参数
String postDate = "txtName=zzz&QueryTypeLst=1&CertificateTxt=dsds";
//由于webView.postUrl(url, postData)中 postData类型为byte[] ,
//通过EncodingUtils.getBytes(data, charset)方法进行转换
webView.postUrl(url, EncodingUtils.getBytes(postDate, "BASE64"));
不行啊 我要请求的参数是json格式的 EncodingUtils.getBytes(postDate, "BASE64")会报错 “The method getBytes(String, String) in the type EncodingUtils is not applicable for the arguments (JSONObject, String)”[/quote] public void postUrl(String url, byte[] postData) 这个是方法的原始签名 你将你的json 对象转成String 再转成byte就行了
NASA_001 2015-07-05
  • 打赏
  • 举报
回复
引用 2 楼 hjq2013 的回复:
//需要访问的网址
String url = "http://www.cqjg.gov.cn/netcar/FindThree.aspx";
//post访问需要提交的参数
String postDate = "txtName=zzz&QueryTypeLst=1&CertificateTxt=dsds";
//由于webView.postUrl(url, postData)中 postData类型为byte[] ,
//通过EncodingUtils.getBytes(data, charset)方法进行转换
webView.postUrl(url, EncodingUtils.getBytes(postDate, "BASE64"));
不行啊 我要请求的参数是json格式的 EncodingUtils.getBytes(postDate, "BASE64")会报错 “The method getBytes(String, String) in the type EncodingUtils is not applicable for the arguments (JSONObject, String)”
Hare_ 2015-07-05
  • 打赏
  • 举报
回复
//需要访问的网址
String url = "http://www.cqjg.gov.cn/netcar/FindThree.aspx";
//post访问需要提交的参数
String postDate = "txtName=zzz&QueryTypeLst=1&CertificateTxt=dsds";
//由于webView.postUrl(url, postData)中 postData类型为byte[] ,
//通过EncodingUtils.getBytes(data, charset)方法进行转换
webView.postUrl(url, EncodingUtils.getBytes(postDate, "BASE64"));
Hare_ 2015-07-05
  • 打赏
  • 举报
回复
//需要访问的网址
String url = "http://www.cqjg.gov.cn/netcar/FindThree.aspx";
//post访问需要提交的参数
String postDate = "txtName=zzz&QueryTypeLst=1&CertificateTxt=dsds";
//由于webView.postUrl(url, postData)中 postData类型为byte[] ,
//通过EncodingUtils.getBytes(data, charset)方法进行转换
webView.postUrl(url, EncodingUtils.getBytes(postDate, "BASE64"));
Hare_ 2015-07-05
  • 打赏
  • 举报
回复
可以把, http://zyfromcq.iteye.com/blog/1354391

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧