0

What I was doing: I was using android http library to make http requests

What i am doing: I have migrated into Oktttp now and i am using below code


In doInBackground of an AsyncTask i am calling the below function

public static String getRequestNoPayload(String urlString) throws Exception {

        client.setConnectTimeout(20, TimeUnit.SECONDS); // connect timeout
        client.setReadTimeout(20, TimeUnit.SECONDS);    // socket timeout
        Request request = new Request.Builder()
        .url(urlString)
        .addHeader("phonenumber",AppController.getPhoneNumber())
        .addHeader("authtoken",AppController.getAuthCode())
        .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }

value of url:

String urlString=Keys.login_api+"?phonenumber="+edtPhnoId.getText().toString().trim();

What is happening:

  • Not able to send requests like this since i am appending the url with a param ?
  • How to resolve this ... should i go for any specific encoding methods, if so which is that one
  • Any sample would help
Devrath
  • 42,072
  • 54
  • 195
  • 297

1 Answers1

0

Try this

RequestBody formBody = new FormEncodingBuilder()  
  .add("search", "Jurassic Park")
  .build();
Request request = new Request.Builder()  
  .url("https://en.wikipedia.org/w/index.php")
  .post(formBody)
  .build();

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

https://publicobject.com/2014/06/12/okhttp-ate-mimecraft/

Fahim
  • 12,198
  • 5
  • 39
  • 57