在Android的OKhttp中通过POST请求发送JSON正文

nifCody

我已经设置好OkHttpClient并成功将GET请求发送到服务器。而且,我还可以将POST请求发送到带有空body标签的服务器。

现在,我正在尝试将以下JSON对象发送到服务器。

{
"title": "Mr.",
"first_name":"Nifras",
"last_name": "",
"email": "[email protected]",
"contact_number": "75832366",
"billing_address": "",
"connected_via":"Application"
}

为此,我尝试添加OkHttpClient库类,RequestBody但无法将JSON对象作为http POST请求的正文发送。我尝试通过以下方式构建主体并处理发布请求。

OkHttpClient client = new OkHttpClient();

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return ApplicationContants.JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
              // This is the place to add json I thought. But How could i do this
        }
    };

    Request request = new Request.Builder()
            .url(ApplicationContants.BASE_URL + ApplicationContants.CUSTOMER_URL)
            .post(body)
            .build();

通过POST请求将JSON对象发送到服务器的方式是什么。

提前致谢。

梅布尔·约翰

试试这个

添加Gradle取决于 compile 'com.squareup.okhttp3:okhttp:3.2.0'

public static JSONObject foo(String url, JSONObject json) {
        JSONObject jsonObjectResp = null;

        try {

            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            OkHttpClient client = new OkHttpClient();

            okhttp3.RequestBody body = RequestBody.create(JSON, json.toString());
            okhttp3.Request request = new okhttp3.Request.Builder()
                    .url(url)
                    .post(body)
                    .build();

            okhttp3.Response response = client.newCall(request).execute();

            String networkResp = response.body().string();
            if (!networkResp.isEmpty()) {
                jsonObjectResp = parseJSONStringToJSONObject(networkResp);
            }
        } catch (Exception ex) {
            String err = String.format("{\"result\":\"false\",\"error\":\"%s\"}", ex.getMessage());
            jsonObjectResp = parseJSONStringToJSONObject(err);
        }

        return jsonObjectResp;
    }

解析响应

   private static JSONObject parseJSONStringToJSONObject(final String strr) {

    JSONObject response = null;
    try {
        response = new JSONObject(strr);
    } catch (Exception ex) {
        //  Log.e("Could not parse malformed JSON: \"" + json + "\"");
        try {
            response = new JSONObject();
            response.put("result", "failed");
            response.put("data", strr);
            response.put("error", ex.getMessage());
        } catch (Exception exx) {
        }
    }
    return response;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法通过OkHttp在android系统发送POST请求

检索Play框架Java中POST请求中发送的请求正文字符串

从Android发送JSON HTTP POST请求

通过resttemplate Exchange在POST请求的正文中发送参数和列表

解析POST请求中发送的json正文数组,并在GoLang中打印

Android Java发送JSON发布请求,正文为空

如何通过在Okhttp中传递对象来发出POST请求?

无法将JSON作为正文发送给GO中的HTTP POST请求

带JSON正文的POST请求

如何使用Alamofire 4在Swift 3中发送带有参数和正文的JSON数据的POST请求?

如何在Slim中访问POST请求的JSON请求正文?

OkHttp POST似乎不发送请求正文

如何使用Volley发送带有JSON正文的POST请求?

Android Volley Post请求在正文中带有json对象,并在String中获取响应

使用Retrofit Android在POST请求中从JSON正文中排除某些字段

使用JSON正文发送POST请求时出现400响应

Flutter POST请求正文未发送

如何从Akka HTTP POST请求读取JSON正文并将最终响应作为JSON数组发送

如何在Qt中使用JSON正文发送POST请求

使用Guzzlehttp发送带有JSON正文的POST请求

使用HTTP get请求发送JSON正文

在POST请求中从正文检索JSON-Java

POST请求未发送任何请求正文

Runscope –如何通过POST请求发送JSON正文?

Android:HttpUrlConnection使用POST发送请求正文(而不是GET)

带有标题和 JSON 正文的 Volley 中的 POST 请求

具有许多孩子的 okHTTP POST 请求正文

POST 请求的 JSON 正文

从 Java 中的 Curl Post 请求中获取 Json / Resonse 正文