我有一个这样的示例 url:
http://www.sample.com/mobile.cgi?action=login&json_request= {"user":{"name":"a","pass":"123","gender":"m","age":"25"}}
我基本上可以使用 webview.loadurl
将数据发送到服务器,但重点是......我无法从服务器获得响应。我是 json 的新手。有什么方法可以使用常规方式发布 json 吗?像 HttpPost
也许吧?并能够得到正确的响应。
谢谢!!
最佳答案
如果您只想使用 HTTP
发布 JSON 并获得响应,stackoverflow 上有很多帖子可以帮助您解决这个问题。检查How to send POST request in JSON using HTTPClient?问题。我认为这个问题回答了你想说的话。希望这对你有帮助。如果您有任何具体问题,可以随时发表评论。
更新 正如您所说,除了 URL 之外,您已经有了键和相应的值。
第一步是创建一个 JSON 对象。将其转换为字符串,然后您可以使用 HTTPClient
发送它并获取响应。像这样的东西:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/");
try {
// Add your data
JSONObject user = new JSONObject();
user.put("Name", "a");
user.put("pass", "123");
// Create StringEntity
StringEntity se = new StringEntity( user.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
您可以查看类似 this 的链接查看更多确切格式。我想告诉你如何继续的方法。希望这可以帮助。
关于android webview 使用 json 发布和响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16702202/