简体   繁体   中英

How to make a simple http post request in Android?

I am a beginner in android. I do have some experience with java, but can not solve this problem for some time.

Here is some background information: I am trying to make a mobile application, which picks an image from the gallery or camera then send it to my server. I am done with picking the image, converting it to base64, but somehow I can not complete the function for http post request. I want this function to return the response from the server, but I am failing.

I have highlighted a part of the function with a comment. The part I do not understand is Overridden methods. As I said, I am a beginner and Android Studio made me include these methods(OnResponse, OnFailure), with these methods I can not return the response because they are void; and without these methods I am having an exception which I have no clue where it is coming from.

public void postRequest(String postdata) throws IOException {
        MediaType MEDIA_TYPE = MediaType.parse("application/json");
        String url = "https://ptsv2.com/t/z497u-xxx/post";

        OkHttpClient client = new OkHttpClient();

        RequestBody body = RequestBody.create(MEDIA_TYPE, postdata);

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        //THIS IS THE PART I DO NOT UNDERSTAND
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                message = response.body().string();
            }
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }
        });
    }

The enqueue function performs the network access on a background thread. Therefore the result is not immediately available to you. To receive the result you must pass in an instance of Callback . When the request finishes in the other thread OkHttp will call the onResponse function of your callback instance. If the request failed it will call onFailure .

If you need the result in the calling thread and block until the result is available you can use the execute() method of the call class. Note that this will not work on the main thread in Android as network access is not allowed there.

Retrofit 2: Get JSON from Response body

Go through above link if you want to post then just pass parameter to interface like mentioned follow

in api interface write

@FormUrlEncoded
@POST("append_url")
Call<CompanyDetailsResponse> getCompanyDetails(@Field("para") String para);

and use

Call<CompanyDetailsResponse> call = apiService.getCompanyDetails("para");

instead of

Call<CompanyDetailsResponse> call = apiService.getCompanyDetails();

and use model class according to your json response and drop comment if any query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM