1

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?

Solomon
  • 169
  • 1
  • 4
  • 10
  • You should post the stacktrace - or at least state what the error is... – Jim Nov 08 '15 at 18:56
  • you can't do both network and set texts on the same thread. Your error is probably a trivial network on main thread error.\ – njzk2 Nov 09 '15 at 00:45

2 Answers2

1

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 {
                             }
                         }
                     }
DoronK
  • 4,837
  • 2
  • 32
  • 37
0

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!

Community
  • 1
  • 1
BNK
  • 23,994
  • 8
  • 77
  • 87