如何使用 Android 通过 HttpClient 请求发送 JSON 对象?

发布于 2025-01-08 02:21:24 字数 1035 浏览 0 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下雨或天晴 2025-01-15 02:21:24

我这样做的

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

另外,new HttpPost() 将 Web 服务 URL 作为参数。

I do this with

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

Also, the new HttpPost() takes the web service URL as argument.

酷到爆炸 2025-01-15 02:21:24

在 try catch 循环中,我这样做了:

         HttpPost post = new HttpPost(
         "https://www.placeyoururlhere.com");
         post.setHeader(HTTP.CONTENT_TYPE,"application/json" );
         List<NameValuePair> nameValuePairs = new
         ArrayList<NameValuePair>(1);
         nameValuePairs.add(new BasicNameValuePair("json", json));
         post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         HttpClient client = new DefaultHttpClient();
         HttpResponse resp = client.execute(post);
         HttpEntity entity = resp.getEntity();

         response = EntityUtils.toString(entity);

您可以根据您拥有的字段数量添加 nameValurPairs。
通常,JSON 可能会变得非常大,然后我建议对其进行 gzip 压缩然后发送,但如果您的 JSON 相当小并且大小始终相同,则上述内容应该适合您。

In the try catch loop, I did this:

         HttpPost post = new HttpPost(
         "https://www.placeyoururlhere.com");
         post.setHeader(HTTP.CONTENT_TYPE,"application/json" );
         List<NameValuePair> nameValuePairs = new
         ArrayList<NameValuePair>(1);
         nameValuePairs.add(new BasicNameValuePair("json", json));
         post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         HttpClient client = new DefaultHttpClient();
         HttpResponse resp = client.execute(post);
         HttpEntity entity = resp.getEntity();

         response = EntityUtils.toString(entity);

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.

你的呼吸 2025-01-15 02:21:24

如果它是一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文