Solomon
Solomon

Reputation: 199

Android OKHTTP Post Request

I am currently having errors executing my post request. I tried to do what others recommended on other stackoverflow links but it did not work for me. Below is my code:

public void postGames(View v) {
    //Sets the data input by user into variables
    TextView gameName = (TextView) findViewById(R.id.gameName);
    TextView companyName = (TextView) findViewById(R.id.companyName);
    TextView consoleName = (TextView) findViewById(R.id.consoleName);
    final TextView resultMessage = (TextView) findViewById(R.id.postResult);

    //Set up the url
    String gamesUrl = "sampleurl...";

    //Creates the HTTP client and puts the url info in it
    OkHttpClient client = new OkHttpClient();
    RequestBody formBody = new FormEncodingBuilder()
            .add("name", "hello")
            .add("company", "world")
            .add("console", "Blagh")
            .build();
    Request request = new Request.Builder()
            .url(gamesUrl)
            .post(formBody)
            .build();

    //First checks if the network is available
    if (isNetworkAvailable()) {
        //Executes the post request
        try {
            Response response = client.newCall(request).execute();
            if(response.isSuccessful()){
                resultMessage.setText("Post successful...");
            }
        } catch (IOException e) {
            e.printStackTrace();
            resultMessage.setText("Error in executing post request...");
        }

    } else {
        Toast.makeText(this, getString(R.string.network_unavailable_message),
                Toast.LENGTH_LONG).show();
    }
}

It gives the error on the "Response response = client.newCall(request).execute();" line. Is there something that I'm doing wrong here?

Upvotes: 1

Views: 1420

Answers (2)

BNK
BNK

Reputation: 24114

Since you are using synchronous way of OkHttp, so in Android you must use it inside AsyncTask. Your app could perhaps get NetworkOnMainThreadException already.

If you don't want AsyncTask, you should implement OkHttp with its asynchronous way.

IMHO, you can find more information at

https://github.com/square/okhttp/wiki/Recipes

and there's a good answer at the following question available on SO:

Using OKHttp, what is the difference between synchronous request in AsyncTask and OKhttp Asynchronous request?

Hope this helps!

Upvotes: 0

DoronK
DoronK

Reputation: 4837

Try to implement the response in this way:

final Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
                         @Override
                         public void onFailure(Request request, final I
                                 }
                             });
                         }
                         @Override
                         public void onResponse(final Response response)
                                 throws IOException {
                             }
                         }
                     }

Upvotes: 1

Related Questions