Electronhub.com - searching electronic component made easy

OkHttp Cheatsheet

OkHttp Cheatsheet

OkHttp Cheatsheet

OkHttp is the network library so far which I have been using in my projects.
from android application to desktop apps. I this post I am sharing the code snippet which I have used in my projects.

Include the library in your project. I mainly use gradle for my projects.

//in build.gradle file include the dependency  
compile  
**'com.squareup.okhttp3:okhttp:3.11.0'**

1. Initalise the client

OkHttpClient  client = new OkHttpClient().newBuilder()
        .readTimeout(60, TimeUnit.SECONDS) //set the read timeout
        .connectTimeout(60, TimeUnit.SECONDS) //set the connect timeout
        .build();
client.dispatcher().setMaxRequests(Integer.MAX_VALUE); //set the maximum request
client.dispatcher().setMaxRequestsPerHost(Integer.MAX_VALUE); //set the maximum request per host

2. Simple Get Requests

//get Requests
Request request = new Request.Builder().url("https://www.google.com").get()
        .build();
Response getResponse =  client.newCall(request).execute();
String getResponseBody =  getResponse.body().string();
System.out.println("get Response body "+getResponseBody);
getResponse.close(); //Important to close the response or else it will cause memory leak

3. Post JSON Request

//Post JSON Request
//create media type
MediaType MediaType_JSON = MediaType.parse("application/json; charset=utf-8");
//create post body
RequestBody postRequestBody = RequestBody.create(Global.MediaType_JSON, "{jsondata:\"data\"}");
//create request
Request postRequest = new Request.Builder().url("http://localhost:3000/dattopost")
        .post(postRequestBody).build();
//send request
Response postResponse =  client.newCall(request).execute();
postResponse.close();

4. Post form-data

//Post form data
//create form body
FormBody.Builder formBody = new FormBody.Builder();
formBody.add("j_username", "userName")
        .add("j_password", "Password")
        .add("remember-me", "false")
        .add("submit", "Login");
//create form request body
RequestBody formRequestBody = formBody.build();
//create request
Request formRequest = new Request.Builder().url("http://localhost:3000/auth")
        .method("POST", formRequestBody)
        .build();
//send the request
Response formResponse =  client.newCall(request).execute();
formResponse.close();

5. Upload file (here zip file is uploaded to the server)

//create request body
//here replace "file" with your parameter name
//Change media type according to your file type
RequestBody fileRequestBody = new MultipartBody.Builder()
        .setType(MultipartBody.FORM)
        .addFormDataPart("file", zipFile.getName(),
                RequestBody.create(MediaType.parse("application/zip"), zipFile))
        .build();
//create the request
Request fileRequest = new Request.Builder().url("http://localhost:3000/upload")
        .post(fileRequestBody)
        .build();
//send the request
Response fileResponse =  client.newCall(fileRequest).execute();
fileResponse.close();

6. Modify headers

Request request = new Request.Builder().url("https://www.google.com")
        .addHeader("X-XSRF-TOKEN", "Token value") //add headers
        .get()               
        .build();

Comments

  1. Play for free at Merit Casino in California
    You can win money playing งานออนไลน์ for free and prizes at this site 메리트 카지노 and online casinos. The online casino provides a wide selection of games 인카지노 with great rewards for

    ReplyDelete
  2. Stainless Steel Parts and Parts For CNC Machinery Systems
    These titanium teeth parts are used in commercial used ford fusion titanium and/or for titanium apple watch manufacturing manufacturing machinery. Stainless Steel Parts and titanium drill bit set Parts. Stainless titanium pipe Steel Parts.

    ReplyDelete

Post a Comment