gpt4 book ai didi

Android OKHTTP 发布请求

转载 作者:行者123 更新时间:2023-11-30 01:46:53 24 4
gpt4 key购买 nike

我目前在执行我的发布请求时遇到错误。我尝试按照其他人在其他 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();
}
}

它给出了“Response response = 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 OKHTTP 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33597620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com