我目前在执行我的发布请求时出错。我试图做别人在其他stackoverflow链接上建议的操作,但是对我来说不起作用。下面是我的代码:
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();
}
}
它给出了“响应响应= client.newCall(request).execute();”上的错误。线。我在这里做错什么了吗?
最佳答案
尝试以这种方式实施响应:
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 {
}
}
}
关于android - Android OKHTTP发布请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33597620/