如何使用 Android 通过 HttpClient 请求发送 JSON 对象?
我想将 JSON 文本 {} 发送到网络服务并读取响应。我怎样才能从安卓做到这一点?创建请求对象、设置内容标题等步骤是什么。
我的代码在这里,
public void postData(String result,JSONObject obj) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
String json=obj.toString();
try {
HttpPost httppost = new HttpPost(result.toString());
StringEntity se = new StringEntity(obj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
Log.i("tag", temp);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
我犯了什么错误,请纠正我,因为它显示了一个错误的请求错误 但是当我在海报中发帖时,它显示我的状态为 Successl 200 ok
I want to send the JSON text {} to a web service and read the response. How can I do this from android? What are the steps such as creating request object, setting content headers, etc.
My code is here
public void postData(String result,JSONObject obj) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
String json=obj.toString();
try {
HttpPost httppost = new HttpPost(result.toString());
StringEntity se = new StringEntity(obj.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
Log.i("tag", temp);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
what mistake i have done plz correct me because it shows me an bad request error
but when i do post in poster it shows me status as Successfull 200 ok
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我这样做的
另外,
new HttpPost()
将 Web 服务 URL 作为参数。I do this with
Also, the
new HttpPost()
takes the web service URL as argument.在 try catch 循环中,我这样做了:
您可以根据您拥有的字段数量添加 nameValurPairs。
通常,JSON 可能会变得非常大,然后我建议对其进行 gzip 压缩然后发送,但如果您的 JSON 相当小并且大小始终相同,则上述内容应该适合您。
In the try catch loop, I did this:
You can add your nameValurPairs according to how many fields you have.
Typically the JSON might become really huge, which I will then suggest gzipping it then sending, but if your JSON is fairly small and always the same size the above should work for you.
如果它是一个 Web 服务而不是 RestAPI 调用,那么您可以从服务器获取 WSDL 文件,并使用 SOAP 存根生成器为您完成创建请求对象和网络代码的所有工作,例如 WSClient++
如果您愿意如果你自己做的话事情就会变得有点棘手。 Android 没有附带 SOAP 库。
不过,您可以在此处下载第 3 方库:http://code.google.com/p /ksoap2-android/
如果您需要使用它的帮助,您可能会发现此线程很有帮助:如何使用 KSOAP2 从 Android 调用 .NET Webservice?
如果它是一个 REST-API 调用,比如 POST 或 GET 更具体的话,那么它就非常简单
只需在函数中传递 JSON 格式的字符串对象,并使用 org.json 包来解析响应字符串。
希望这有帮助。
If it is a web service and not RestAPI call then, you can get the WSDL file from the server and use a SOAP Stub generator to do all the work of creating the Request objects and the networking code for you, for example WSClient++
If you wish to do it by yourself then things get a little tricky. Android doesn't come with SOAP library.
However, you can download 3rd party library here: http://code.google.com/p/ksoap2-android/
If you need help using it, you might find this thread helpful: How to call a .NET Webservice from Android using KSOAP2?
If its a REST-API Call like POST or GET to be more specific then its is very simple
Just pass a JSON Formatted String object in you function and use org.json package to parse the response string for you.
Hope this helps.