如何为 OKHTTP 格式编写类似的 Retrofilt 代码?
How to write similar Retrofilt code for OKHTTP format?
我的 OKHttp 代码工作正常,但有时无法进入 ENQUEUE。它没有显示有关它的任何信息。我正在尝试以 Retrofit 语法转换相同的代码。谁能用正确的语法帮助我?
下面file1/2/3是图片文件路径。
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("photos1", String.valueOf(file1),
RequestBody.create(MediaType.parse("application/octet-stream"),
new File(String.valueOf(file2)) ))
.addFormDataPart("photos2", String.valueOf(file2),
RequestBody.create(MediaType.parse("application/octet-stream"),
new File(String.valueOf(file3)) ))
.addFormDataPart("photos3", String.valueOf(file3),
RequestBody.create(MediaType.parse("application/octet-stream"),
new File(String.valueOf(file1)) ))
.addFormDataPart("product_name",txtprodname.getText().toString())
.addFormDataPart("description",txtdesc.getText().toString())
.addFormDataPart("product_cost",txtprice.getText().toString())
.addFormDataPart("tax_value",spntax.getSelectedItem().toString())
.addFormDataPart("within_city_cost",txtcostwithincity.getText().toString())
.addFormDataPart("outside_city_cost",txtcostoutsideincity.getText().toString())
.addFormDataPart("user_id",String.valueOf(user_id))
.addFormDataPart("category_name", spncategory.getSelectedItem().toString())
.addFormDataPart("tax_name",taxname)
.addFormDataPart("business_name", business_name)
.addFormDataPart("youtube_link", txtyoutube.getText().toString())
.addFormDataPart("length", txtL.getText().toString())
.addFormDataPart("width", txtW.getText().toString())
.addFormDataPart("height", txtH.getText().toString())
.addFormDataPart("volumetric_weight", txtwt.getText().toString())
.build();
Request request = new Request.Builder()
.url("https://<myip>/api/productapi/")
.method("POST", body)
.build();
client.newCall(request).enqueue(new okhttp3.Callback() .... rest of the code.
我无法理解 Retrofit 的类似语法。任何帮助将不胜感激。
此致,
PD
如果您需要使用 Retrofit 发送 API'S 中的任何文件,则需要按照以下步骤操作。
- Retrofit初始化创建classAPIClient
public class APIClient {
public static Retrofit retrofit;
public static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).connectTimeout(120, TimeUnit.SECONDS).readTimeout(120, TimeUnit.SECONDS).writeTimeout(120, TimeUnit.SECONDS).build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl("https://myapi.com")
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
}
return retrofit;
}
- 创建一个接口列表API键考虑接口名称APIInterfacr。
最重要的是将 RequestBody 的数据类型分配给所有 String/Int 数据...并分配 MultiPartBody.Part 仅文件..
用户是我的对象 Class 你需要创建你的对象 Class 并替换...
public interface APIInterface {
@Multipart
@POST("xyznode")
Call<Users> updateUser(@Part("id") RequestBody id,
@Part("name") RequestBody name,
@Part("email") RequestBody email,
@Part("password") RequestBody password,
@Part("paypal_address") RequestBody paypal_address,
@Part("user_location") RequestBody user_location,
@Part("role_id") RequestBody role_id,
@Part MultipartBody.Part image_url);
}
- 现在需要调用 API。
public void updateUser(RequestBody id, RequestBody name, RequestBody email, RequestBody password, RequestBody paypal_address, RequestBody user_location, MultipartBody.Part profile_photo_url, RequestBody role) {
if (NetworkConnectivity.isOnline()) {
Call<Users> call = apiInterface.updateUser(id, name, email, password, paypal_address, user_location, role, profile_photo_url);
call.enqueue(new Callback<Users>() {
@Override
public void onResponse(Call<Users> call, Response<Users> response) {
if (response.isSuccessful()) {
//Do your stuff here...
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
//
}
});
}
}
- 最重要的是调用这个特定的函数,为此你需要列出所有需要包裹在你的 API...
中的东西
RequestBody fileReqBody = RequestBody.create(MediaType.parse("image/*"), file);
profileImage = MultipartBody.Part.createFormData("profile_photo_url", file.getName(), fileReqBody);
RequestBody id = RequestBody.create(MediaType.parse("multipart/form-data"), preferencesHandler.getUid());
RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), txt_name);
RequestBody email = RequestBody.create(MediaType.parse("multipart/form-data"), txt_email);
RequestBody password = RequestBody.create(MediaType.parse("multipart/form-data"), txt_password);
RequestBody roleid = RequestBody.create(MediaType.parse("multipart/form-data"), preferencesHandler.getRole());
RequestBody country = RequestBody.create(MediaType.parse("multipart/form-data"), txt_country);
RequestBody paypal = RequestBody.create(MediaType.parse("multipart/form-data"), txt_paypal);
- 现在调用函数..
onUpdateUser(id, name, email, password, paypal, country, roleid, profileImage);
- 要添加到构建文件中的依赖项。
//Retrofit Dependency
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
现在您可以开始了...:-)
我的 OKHttp 代码工作正常,但有时无法进入 ENQUEUE。它没有显示有关它的任何信息。我正在尝试以 Retrofit 语法转换相同的代码。谁能用正确的语法帮助我?
下面file1/2/3是图片文件路径。
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("photos1", String.valueOf(file1),
RequestBody.create(MediaType.parse("application/octet-stream"),
new File(String.valueOf(file2)) ))
.addFormDataPart("photos2", String.valueOf(file2),
RequestBody.create(MediaType.parse("application/octet-stream"),
new File(String.valueOf(file3)) ))
.addFormDataPart("photos3", String.valueOf(file3),
RequestBody.create(MediaType.parse("application/octet-stream"),
new File(String.valueOf(file1)) ))
.addFormDataPart("product_name",txtprodname.getText().toString())
.addFormDataPart("description",txtdesc.getText().toString())
.addFormDataPart("product_cost",txtprice.getText().toString())
.addFormDataPart("tax_value",spntax.getSelectedItem().toString())
.addFormDataPart("within_city_cost",txtcostwithincity.getText().toString())
.addFormDataPart("outside_city_cost",txtcostoutsideincity.getText().toString())
.addFormDataPart("user_id",String.valueOf(user_id))
.addFormDataPart("category_name", spncategory.getSelectedItem().toString())
.addFormDataPart("tax_name",taxname)
.addFormDataPart("business_name", business_name)
.addFormDataPart("youtube_link", txtyoutube.getText().toString())
.addFormDataPart("length", txtL.getText().toString())
.addFormDataPart("width", txtW.getText().toString())
.addFormDataPart("height", txtH.getText().toString())
.addFormDataPart("volumetric_weight", txtwt.getText().toString())
.build();
Request request = new Request.Builder()
.url("https://<myip>/api/productapi/")
.method("POST", body)
.build();
client.newCall(request).enqueue(new okhttp3.Callback() .... rest of the code.
我无法理解 Retrofit 的类似语法。任何帮助将不胜感激。
此致, PD
如果您需要使用 Retrofit 发送 API'S 中的任何文件,则需要按照以下步骤操作。
- Retrofit初始化创建classAPIClient
public class APIClient {
public static Retrofit retrofit;
public static Retrofit getClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).connectTimeout(120, TimeUnit.SECONDS).readTimeout(120, TimeUnit.SECONDS).writeTimeout(120, TimeUnit.SECONDS).build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl("https://myapi.com")
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
}
return retrofit;
}
- 创建一个接口列表API键考虑接口名称APIInterfacr。 最重要的是将 RequestBody 的数据类型分配给所有 String/Int 数据...并分配 MultiPartBody.Part 仅文件.. 用户是我的对象 Class 你需要创建你的对象 Class 并替换...
public interface APIInterface {
@Multipart
@POST("xyznode")
Call<Users> updateUser(@Part("id") RequestBody id,
@Part("name") RequestBody name,
@Part("email") RequestBody email,
@Part("password") RequestBody password,
@Part("paypal_address") RequestBody paypal_address,
@Part("user_location") RequestBody user_location,
@Part("role_id") RequestBody role_id,
@Part MultipartBody.Part image_url);
}
- 现在需要调用 API。
public void updateUser(RequestBody id, RequestBody name, RequestBody email, RequestBody password, RequestBody paypal_address, RequestBody user_location, MultipartBody.Part profile_photo_url, RequestBody role) {
if (NetworkConnectivity.isOnline()) {
Call<Users> call = apiInterface.updateUser(id, name, email, password, paypal_address, user_location, role, profile_photo_url);
call.enqueue(new Callback<Users>() {
@Override
public void onResponse(Call<Users> call, Response<Users> response) {
if (response.isSuccessful()) {
//Do your stuff here...
}
}
@Override
public void onFailure(Call<Users> call, Throwable t) {
//
}
});
}
}
- 最重要的是调用这个特定的函数,为此你需要列出所有需要包裹在你的 API... 中的东西
RequestBody fileReqBody = RequestBody.create(MediaType.parse("image/*"), file);
profileImage = MultipartBody.Part.createFormData("profile_photo_url", file.getName(), fileReqBody);
RequestBody id = RequestBody.create(MediaType.parse("multipart/form-data"), preferencesHandler.getUid());
RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), txt_name);
RequestBody email = RequestBody.create(MediaType.parse("multipart/form-data"), txt_email);
RequestBody password = RequestBody.create(MediaType.parse("multipart/form-data"), txt_password);
RequestBody roleid = RequestBody.create(MediaType.parse("multipart/form-data"), preferencesHandler.getRole());
RequestBody country = RequestBody.create(MediaType.parse("multipart/form-data"), txt_country);
RequestBody paypal = RequestBody.create(MediaType.parse("multipart/form-data"), txt_paypal);
- 现在调用函数..
onUpdateUser(id, name, email, password, paypal, country, roleid, profileImage);
- 要添加到构建文件中的依赖项。
//Retrofit Dependency
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
现在您可以开始了...:-)