繁体   English   中英

如何使用 Retrofit2

[英]How to use Retrofit2

我之前使用 AsyncTask 和 DefaultHttpClient 调用了我的服务,但现在我想使用 Retrofit。 我读到了这个并实现了那个和onResponse方法,但我的响应有错误的请求消息并且无法获得响应正文。

我的错误是:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.retrofitsample.f.retrofitsample/com.retrofitsample.f.retrofitsample.MainActivity}: java.lang.IllegalArgumentException: Unable to create converter for com.retrofitsample.f.retrofitsample.model.HttpResponse<com.retrofitsample.f.retrofitsample.model.MS>

引起:java.lang.IllegalArgumentException:类 com.retrofitsample.f.retrofitsample.model.MS 声明了多个名为 Id 的 JSON 字段

我有一个像下面这样的 wcf 服务:

[OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
       UriTemplate = "GetMS")]
    HttpResponse<MS> GetMS(string token,string Id);

我有一个 HttpResponse 并且我所有的服务都返回一种 T 类型。我在客户端编写了 belo 代码:

我的活动:

retrofit2.Call<HttpResponse<MS>> call=service.getAllMonitoringScheduale("971048F6-7ABA-4060-8CC3-BC57EC259FA3","1292");
    call.enqueue(new Callback<HttpResponse<MS>>() {
        @Override
        public void onResponse(retrofit2.Call<HttpResponse<MS>> call, Response<HttpResponse<MS>> response) {
            Log.e("Response=",response.body().getResultMessage());
        }

        @Override
        public void onFailure(retrofit2.Call<HttpResponse<MS>> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
        }
    });

我的界面:

 public interface GetDataService {

    @POST("GetMonitoringSchedule")
    Call<HttpResponse<MS>> getAllMS(@Query("token")String token,@Query("Id") String Id);
}

这是我的模型类:

    public class HttpResponse<T> {
@SerializedName("ResultMessage")
private String ResultMessage;

@SerializedName("Result")
private T Result;

public HttpResponse(String ResultMessage,T Result){
    this.ResultMessage=ResultMessage;
    this.Result=Result;
}

public String getResultMessage(){
    return ResultMessage;
}
public void setResultMessage(String ResultMessage){
    this.ResultMessage=ResultMessage;
}

public T getResult(){
    return Result;
}
public void setResult(T Result){
    this.Result=Result;
}
 }

public class MS {
@SerializedName("Id")
public long Id;
@SerializedName("PId")
public long PId;
@SerializedName("SType")
public int SType;
@SerializedName("SDateF")
public Date SDateF ;
@SerializedName("SDateT")
public Date SDateT;

..Constructor and setter and getter
}

如何修复此错误以及我的问题是什么? 我不知道如何在 Retrofit2 中使用 HttpResponse 类型?

我找到了一个使用 Retrofit 来发出网络请求的项目。 您可以将其用作参考以了解如何使用 Retrofit。

https://github.com/AcademyTLV/fundamentals-2018-exercise/tree/ex-7-networking

还要确保使用 POJO 为了创建您的类来处理请求: http : //www.jsonschema2pojo.org/

这里我有完整的例子。

这种依赖添加到 gradle

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
annotationProcessor 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

现在在这里创建ApiClient.kt文件

object ApiClient {

val BASE_URL = "http://yourwebsite/services/"
private var retrofit: Retrofit? = null

val client: Retrofit
    get() {
        if (retrofit == null) {
            retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }
        return retrofit!!
    }

}

现在创建APIInterface.kt

@FormUrlEncoded
@POST("users/login")
fun POST_LOGIN(
        @Field("imei_number") imei_number: String,
        @Field("device_token") device_token: String,
        @Field("mobile") mobile: String,
        @Field("password") password: String
): Call<LoginResponse>

@GET("general/init-data")
fun GENERAL_MODULE(
        @Header("Authorization") auth_key: String
): Call<InitResponse>

@GET("event-gallery/list")
fun GET_Event_GALLERY(
        @Header("Authorization") auth_key: String
): Call<EventListResponse>

@GET("event-gallery/photo-list")
fun GET_Event_GALLERY_PHOTO(
        @Header("Authorization") auth_key: String,
        @Query("id") id: Int
): Call<EventGallerListResponse>

如果标记的任何标头使用@Header 并且当调用@GET 时,参数使用@Query 和@Post 时@Fields

现在响应文件

data class EventListResponse(
    @SerializedName("success")
    var success: Boolean,
    @SerializedName("data")
    var data: EventgalleryModel?,
    @SerializedName("server_error"),
    @SerializedName("eventgallery")
    var eventgallery: ArrayList<EventListData>
    var server_error: Boolean,
    @SerializedName("message")
    var message: String

)

然后创建响应的模型类

现在是活动代码的时间

private fun loadData() {
    card_progress.visibility = View.VISIBLE
    val apiService = ApiClient.client.create(ApiInterface::class.java)
    val call =
        apiService.GET_FEE_INSTALMENT_LIST(PreferenceManager.getAuthKey(this@FeesInstalmentActivity)!!)
    call.enqueue(object : Callback<FeeInstalmentListResponse> {
        override fun onResponse(
            call: Call<FeeInstalmentListResponse>,
            response: Response<FeeInstalmentListResponse>
        ) {
            card_progress.visibility = View.GONE
            val data = response.body()!!.data
            if (response.code() == 200 && data != null) {
                if (response.body()!!.server_error) {
                    txt_no_data_fee.visibility = View.VISIBLE
                    txt_no_data_fee.text = response.body()!!.message
                } else {
                    Log.e("data", data.toString())
                    if (data != null && data.feesinstalment.isEmpty()) {
                        txt_no_data_fee.visibility = View.VISIBLE
                    } else {
                        txt_no_data_fee.visibility = View.GONE
                        adapter!!.setItem(data.feesinstalment)
                    }
                }
            } else if (response.code() == 401) {
                PreferenceManager.removePref(this@FeesInstalmentActivity)
                startActivity(
                    Intent(this@FeesInstalmentActivity, LoginActivity::class.java)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
                )
                finish()
            } else {
                Toast.makeText(
                    this@FeesInstalmentActivity,
                    R.string.somethingWrong,
                    Toast.LENGTH_SHORT
                ).show()
            }


        }

        override fun onFailure(call: Call<FeeInstalmentListResponse>, t: Throwable) {
            card_progress.visibility = View.GONE
            Log.e("onFailure", t.message)
            txt_no_data_fee.visibility = View.VISIBLE
        }
    })
}

Sry 我忘了这里的适配器

class FeeInstalmentAdapter(
    private val context: Context,
    private var items: ArrayList<FeeInstalmentListData>

) : RecyclerView.Adapter() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    return ViewHolder(LayoutInflater.from(context).inflate(R.layout.row_fees_instalment_item, parent, false))
}

@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.due_date.text = DateHelper.parseData(items[position].due_date!!, "yyyy-MM-dd", "dd MMM yyyy")
    holder.instalment_title.text = items[position].instalment_title
    if (items[position].paid_date == null) {
        holder.paid_text.visibility = View.GONE
        holder.paid_date.text = context.resources.getString(R.string.UnPaid)
        holder.paid_date.setTextColor(Color.parseColor("#DC143C"))
    } else {
        holder.paid_date.text = DateHelper.parseData(items[position].due_date!!, "yyyy-MM-dd", "dd MMM yyyy")
        holder.paid_date.setTextColor(Color.parseColor("#58A259"))
    }
    //holder.paid_date.text = items[position].paid_date
    holder.amount.text = "Rs. " + items[position].amount
    holder.amount.setTextColor(Color.parseColor("#ED7136"))

}


override fun getItemCount(): Int {
    return items.size
}

override fun getItemId(position: Int): Long {
    return position.toLong()
}

override fun getItemViewType(position: Int): Int {
    return position
}

fun setItem(holidays: ArrayList<FeeInstalmentListData>) {
    items = holidays
    notifyDataSetChanged()
}


class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
    val due_date = view.due_date
    val instalment_title = view.instalment_title
    val paid_date = view.paid_date
    val amount = view.amount
    val paid_text = view.paidText
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM