要发送异步请求,请使用以下命令:
void doGetRequest(String url) throws IOException{ Request request = new Request.Builder() .url(url) .build(); client.newCall(request) .enqueue(new Callback() { @Override public void onFailure(final Call call, IOException e) { // Error runonUiThread(new Runnable() { @Override public void run() { // For the example, you can show an error dialog or a toast // on the main UI thread } }); } @Override public void onResponse(Call call, final Response response) throws IOException { String res = response.body().string(); // Do something with the response } });}
并这样称呼:
case R.id.btLogin: try { doGetRequest("http://myurl/api/"); } catch (IOException e) { e.printStackTrace(); } break;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)