使用 retrofit2 和 Okhttp 获取 Api
Posted
技术标签:
【中文标题】使用 retrofit2 和 Okhttp 获取 Api【英文标题】:Using retrofit2 and Okhttp to GET Api 【发布时间】:2020-07-08 04:12:50 【问题描述】:我正在尝试使用 GET 方法在布局中显示我的产品数据。我可以使用 POST 方法上传产品,它工作正常,但使用 GET 不工作 这是我的 ApiService 接口代码
我从 l 收到此错误
Java
public interface ApiService
@Multipart
@POST("/se/product")
/* @Headers("Content-Type: application/json")*/
Call<Product> saveProduct(@Part List<MultipartBody.Part> file, @Part("name") RequestBody name, @Part("price") RequestBody price, @Part("quantity") RequestBody quantity, @Part("idCategory") RequestBody idCategory,
@Part("model") RequestBody model, @Header("Authorization") String token);
@GET("/se/product/productId")
Call<Product> getProduct(@Body RequestBody id,@Header("Authorization") String token);
// @GET("/se/product/productId")
// public void getProductDetails(@Part("product_id") String product_id, Callback<Product> callback);
这是我的产品课
public class ProductsDetails extends AppCompatActivity
ApiService chuckService;
TextView price,quantity,model,name;
ImageView file;
String token;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_details);
name = (TextView) findViewById(R.id.name);
model = (TextView) findViewById(R.id.model);
price = (TextView) findViewById(R.id.price);
quantity = (TextView) findViewById(R.id.quantity);
file = (ImageView) findViewById(R.id.file);
ApiService chuckService = Okhttp_RetrofitClient.getClient().create(ApiService.class);
JSONObject paramObject = new JSONObject();
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), String.valueOf((paramObject)));
Call<Product> call = chuckService.getProduct(body,token);
call.enqueue(new Callback<Product>()
@Override
public void onResponse(Call<Product> call,
Response<Product> response)
name.setText(response.body().getName());
price.setText(response.body().getPrice());
quantity.setText(response.body().getQuantity());
model.setText(response.body().getModel());
Picasso.with(getApplicationContext()).load(response.body().getFile()).
into(file);
@Override
public void onFailure(Call<Product> call, Throwable
t)
name.setText(t.getMessage());
);
这是我的产品 POJO
public class Product
@Override
public String toString()
return "Product" +
"id=" + id +
", quantity=" + quantity +
", idCategory=" + idCategory +
", name='" + name + '\'' +
", model='" + model + '\'' +
", published=" + published +
", price=" + price +
'';
private String productId;
public String getProductId()
return productId;
public void setProductId(String productId)
this.productId = productId;
public List<String> getImages()
return images;
public void setImages(List<String> images)
this.images = images;
private List<String> images = null;
public Integer getId()
return id;
public void setId(Integer id)
this.id = id;
private Integer id;
@SerializedName("quantity")
@Expose
private String quantity;
@SerializedName("idCategory")
@Expose
private Integer idCategory;
public String getQuantity()
return quantity;
@SerializedName("name")
@Expose
private String name;
@SerializedName("model")
@Expose
private String model;
public void setQuantity(String quantity)
this.quantity = quantity;
public Integer getIdCategory()
return idCategory;
public void setIdCategory(Integer idCategory)
this.idCategory = idCategory;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getModel()
return model;
public void setModel(String model)
this.model = model;
public boolean isPublished()
return published;
public void setPublished(boolean published)
this.published = published;
public Integer getPrice()
return price;
public void setPrice(Integer price)
this.price = price;
public String getFile()
return file;
public void setFile(String file)
this.file = file;
@SerializedName("published")
@Expose
private boolean published;
@SerializedName("price")
@Expose
private Integer price;
@SerializedName("file")
@Expose
private String file;
这是查看产品详细信息的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:id="@+id/detail_product"
android:layout_
android:layout_
android:layout_margin="16dp">
<LinearLayout
android:layout_
android:layout_
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_
android:layout_
android:layout_margin="5dp"
android:text="Nom du produit"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="italic" />
<LinearLayout
android:layout_
android:layout_
android:layout_margin="5dp"
android:layout_weight="2"
android:orientation="horizontal">
<ImageView
android:id="@+id/file"
android:layout_
android:layout_
android:layout_margin="5dp"
android:layout_weight="1" />
<TextView
android:id="@+id/model"
android:layout_
android:layout_
android:layout_margin="5dp"
android:layout_weight="1"
android:text="Details"
android:textAlignment="textStart"
android:textColor="@android:color/black"
android:textSize="12sp" />
</LinearLayout>
<RelativeLayout
android:layout_
android:layout_>
<TextView
android:id="@+id/price"
android:layout_
android:layout_
android:layout_margin="5dp"
android:text="Cfa. 190.000"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="italic" />
<TextView
android:id="@+id/quantity"
android:layout_
android:layout_
android:layout_margin="5dp"
android:layout_toRightOf="@id/price"
android:text="Quantite"
android:textColor="@android:color/black"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
堆栈跟踪
Caused by: java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body. for method
ApiService.getProduct at
retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:755) at com.monsokoseller.app.Activities.ProductsDetails.onCreate(ProductsDetails.java:50)
【问题讨论】:
我从 logcat 收到此错误原因:java.lang.IllegalArgumentException:非正文 HTTP 方法不能包含 @Body。对于 com.monsokoseller.app.Activities.ProductsDetails.onCreate(ProductsDetails.java:50) 的 retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:755) 方法 ApiService.getProduct 不应该用post代替get吗? @Raghunandan 我想在发布后查看产品信息,POST 方法不起作用。 【参考方案1】:GET 请求不能包含请求体。 api 不能包含带有请求正文的 GET-Request。请再次查看 api 文档。
您可以使用curl 试用 API。你到底想在体内提供什么?如果只需要提供productId
,接口声明可以改为:
@GET("/se/product/productId")
Call<Product> getProduct(@Path("productId") productId,@Header("Authorization") String token);
此调用将请求路径中的变量 productId
替换为实际的产品 ID。
【讨论】:
感谢您的回复,您能给我举个例子吗?以上是关于使用 retrofit2 和 Okhttp 获取 Api的主要内容,如果未能解决你的问题,请参考以下文章
Retrofit2.0+OkHttp打印Request URL(请求地址参数)
Retrofit2.0+OkHttp打印Request URL(请求地址参数)