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?