简体   繁体   English

如何使用okhttp在android中将web api(post request)URL参数作为requestbody传递

[英]how to pass web api(post request) url parameters as requestbody in android using okhttp

I have a asp.net web api url which accepts query string parameters but its actually a post request. 我有一个asp.net Web API URL,它接受查询字符串参数,但实际上是一个发布请求。 I am trying to access the same url in android but not sure how to pass the query parameters. 我正在尝试在android中访问相同的url,但不确定如何传递查询参数。 Any help will be appreciated as I am not an android developer basically. 任何帮助将不胜感激,因为我基本上不是Android开发人员。

Here is the snippet: 这是代码段:

     RequestBody formBody = new FormBody.Builder()
                    .add("email", mEmail.toLowerCase())
                    .add("password", mPassword)
                    .build();

            //2. Bind the request Object
            Request req = new Request.Builder()
                    .url(loginAPI).post(formBody)
                    .build();
            Response response = client.newCall(req).execute();

This is the solution: 这是解决方案:

String loginAPI = "http://api.myapi.com/api/authentication?email="+mEmail.toLowerCase()+"&password="+mPassword;

            RequestBody reqbody = RequestBody.create(null, new byte[0]);
           Request  req = new Request.Builder()
                    .url(loginAPI)
                    .method("POST", reqbody)
                    .build();

            Response response = client.newCall(req).execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中使用okhttp库向api(http post)添加参数 - How to add parameters to api (http post) using okhttp library in Android 正确的语法,用于在Android中使用okhttp向get()和post()请求添加参数 - Correct syntax for adding parameters to get() and post() request using okhttp in Android 如何在 Retrofit - Android 中将参数传递给 POST 请求 - How to pass parameters to POST request in Retrofit - Android Android-使用okHttp通过发布请求发送图像 - Android - sending image by post request using okHttp Android OKHTTP发布请求 - Android OKHTTP Post Request 如何使用 Android Studio 和 Web Api 使用相同的请求 POST 和 GET - How to POST And GET with the same Request Using Android Studio And Web Api How to get api request and api response time in Android Retrofit api using OkHttp Interceptor for each api? - How to get api request and api response time in Android Retrofit api using OkHttp Interceptor for each api? 如何使用 Retrofit 将参数传递给 POST 请求,然后序列化请求? - How to pass parameters to POST request using Retrofit and then serialize request? Android发布请求因OKHTTP失败 - Android post request failed with OKHTTP 使用 OKHTTP3 向 Web 服务器发送 POST 请求 - Sending a POST request to a web server using OKHTTP3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM