import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response;
时间: 2023-11-17 14:08:42 浏览: 185
这是Java中使用OkHttp库发送HTTP请求所需的导入语句。OkHttp是一个非常流行的HTTP客户端库,用于在Android和Java应用程序中进行网络请求。它提供了简单易用的API,使得发送HTTP请求变得非常容易。这些导入语句用于创建OkHttpClient对象、Request对象和发送请求。其中,OkHttpClient是OkHttp库的核心组件,用于处理HTTP请求和响应。Request对象用于描述HTTP请求,包括URL、请求方法、请求头、请求体等信息。Response对象则包含HTTP响应的信息,包括状态码、响应头、响应体等。同时,使用Callback接口可以异步处理HTTP响应。
相关问题
package com.example.ibm; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.google.gson.Gson; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private EditText etHeight; private EditText etWeight; private Button btnCalculate; private TextView tvResult; private OkHttpClient client; private Gson gson; private static final String API_KEY = "d889e63ef7ad041b837dcc1b3ebc5a2b"; private static final String API_URL = "http://apis.juhe.cn/fapig/calculator/weight"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etHeight = findViewById(R.id.et_height); etWeight = findViewById(R.id.et_weight); btnCalculate = findViewById(R.id.btn_calculate); tvResult = findViewById(R.id.tv_result); client = new OkHttpClient(); gson = new Gson(); btnCalculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String height = etHeight.getText().toString(); String weight = etWeight.getText().toString(); if (height.isEmpty() || weight.isEmpty()) { tvResult.setText("请输入身高和体重"); return; } float h = Float.parseFloat(height); float w = Float.parseFloat(weight); if (h <= 0 || w <= 0) { tvResult.setText("身高和体重必须大于0"); return; } calculateBMI(h, w); } }); }
这是一个 Android 应用程序的 Java 代码,包括一个 MainActivity 类来处理用户界面和计算BMI的逻辑。在 onCreate 方法中,它首先通过 findViewById 方法获取布局文件中的各个控件,并创建了一个 OkHttpClient 对象和一个 Gson 对象。然后,它为“计算BMI”按钮设置了一个点击事件监听器,当用户点击按钮时,将从身高和体重 EditText 中获取输入的值,然后进行一些检查,如确保输入的值是数字且大于0。如果输入无效,则在 TextView 中显示相应的错误消息。否则,它将调用 calculateBMI 方法来计算BMI。其中,它使用 OkHttp 库来向 API 发送 HTTP 请求,并将 API 返回的 JSON 数据解析为一个对象,最后将计算结果显示在 TextView 中。
package com.hmongsoft.merchant.Module.dataSource.onLine.V20230707; import android.os.AsyncTask; import com.hmongsoft.merchant.Base.Interface.ActionCallbackValue; import com.hmongsoft.merchant.Base.config.SysConfig; import java.io.IOException; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; /** * 校验商铺名称是否存在 */ public class JudgeMerchantIsExist { public JudgeMerchantIsExist(String merchantName, ActionCallbackValue<String,Boolean> actionCallback) { SignInTask signInTask=new SignInTask(merchantName,actionCallback); signInTask.execute(); } private static class SignInTask extends AsyncTask<String,Integer,Boolean>{ private String merchantName; private ActionCallbackValue<String,Boolean> actionCallback; private String requestResult; public SignInTask(String merchantName, ActionCallbackValue<String,Boolean> actionCallback) { this.merchantName =merchantName; this.actionCallback=actionCallback; } //异步前(UI) @Override protected void onPreExecute() { super.onPreExecute(); } //异步中(非UI) @Override protected Boolean doInBackground(String... strings) { OkHttpClient client = new OkHttpClient().newBuilder().build(); RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM) .addFormDataPart("merchantName",merchantName) .build(); Request request = new Request.Builder() .url(SysConfig.MerchantPORT+"/MerchantController/judgeMerchantNameIsExist") .method("POST", body) .build(); try { requestResult = client.newCall(request).execute().body().string(); if (requestResult.equals("true")){ return true; }else { return false; } } catch (IOException e) { e.printStackTrace(); return false; } } //异步后(UI) @Override protected void onPostExecute(Boolean aBoolean) { super.onPostExecute(aBoolean); actionCallback.callback("result",aBoolean); } } } 这段代码的SignInTask已经被弃用,请帮换一个写法
package com.hmongsoft.merchant.Module.dataSource.onLine.V20230707;
import android.os.AsyncTask;
import com.hmongsoft.merchant.Base.Interface.ActionCallbackValue;
import com.hmongsoft.merchant.Base.config.SysConfig;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
/**
* 校验商铺名称是否存在
*/
public class JudgeMerchantIsExist {
public JudgeMerchantIsExist(String merchantName, ActionCallbackValue<String, Boolean> actionCallback) {
new SignInTask(merchantName, actionCallback).execute();
}
private static class SignInTask extends AsyncTask<Void, Void, Boolean> {
private String merchantName;
private ActionCallbackValue<String, Boolean> actionCallback;
public SignInTask(String merchantName, ActionCallbackValue<String, Boolean> actionCallback) {
this.merchantName = merchantName;
this.actionCallback = actionCallback;
}
@Override
protected Boolean doInBackground(Void... voids) {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
String json = "{\"merchantName\":\"" + merchantName + "\"}";
RequestBody body = RequestBody.create(json, mediaType);
Request request = new Request.Builder()
.url(SysConfig.MerchantPORT + "/MerchantController/judgeMerchantNameIsExist")
.post(body)
.build();
try {
String response = client.newCall(request).execute().body().string();
return response.equals("true");
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
actionCallback.callback("result", result);
}
}
}
阅读全文