如果没有响应,应用程序将在此行崩溃。
optionsArray : Array<{productOptionId : string , productOptionValueId : string}> = [];
这是我的RetrofitClient课程
chain.proceed(requestBuilder.build())
这是日志中的错误:
class RetrofitClient {
private val apiService: ApiServiceInterface
init {
val builder = Retrofit.Builder()
builder.baseUrl(RequestParameters.BASE_URL)
builder.addConverterFactory(GsonConverterFactory.create())
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//added for adapter
builder.client(getClient())
val retrofit = builder.build()
apiService = retrofit.create(ApiServiceInterface::class.java)
}
companion object {
private var clientInstance: RetrofitClient? = null
lateinit var context: Context
fun getInstance(): RetrofitClient {
if (clientInstance == null) {
clientInstance = RetrofitClient()
}
return clientInstance as RetrofitClient
}
}
fun getApiInterface(): ApiServiceInterface {
return apiService
}
private fun getClient(): OkHttpClient {
val httpClient = OkHttpClient.Builder()
httpClient.readTimeout(25, TimeUnit.SECONDS)
httpClient.connectTimeout(25, TimeUnit.SECONDS)
httpClient.addInterceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
.header(RequestParameters.X_API, RequestParameters.H_XAPI_KEY)
.method(original.method(), original.body())
chain.proceed(requestBuilder.build())
}
// set your desired log level
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.HEADERS
logging.level = HttpLoggingInterceptor.Level.BODY
httpClient.addInterceptor(logging)
return httpClient.build()
}
}
答案 0 :(得分:0)
尝试这种代码方式。 成为改造对象创建的一部分。
interface ApiInterface {
@GET("marvel")
fun getData(): Call<List<Hero>>
}
用于api调用的make接口。
var apiInterface: ApiInterface = ApiClient.getClient()!!.create(ApiInterface::class.java)
var hero: Call<List<Hero>>
hero = apiInterface.getData()
hero.enqueue(object : Callback<List<Hero>> {
override fun onFailure(call: Call<List<Hero>>?, t: Throwable?) {
closeDialog(dialog)
Toast.makeText(mContext, t?.message, Toast.LENGTH_SHORT).show()
Log.d("Error:::",t?.message)
}
override fun onResponse(call: Call<List<Hero>>?, response: Response<List<Hero>>?) {
mHeroDataList.clear()
if (response != null && response.isSuccessful && response.body() != null) {
closeDialog(dialog)
mHeroDataList .addAll(response.body()!!)
setAdapter(mHeroDataList)
}
}
})
}
像这样,在活动或称为api的片段中,我希望您创建响应pojo类
public class GradientTextView extends AppCompatTextView {
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.GradientTextView);
int startColor = a.getColor(R.styleable.GradientTextView_startColor, Color.WHITE);
int endColor = a.getColor(R.styleable.GradientTextView_endColor, Color.WHITE);
Shader myShader = new LinearGradient(0, 0, 0, 100,startColor, endColor, Shader.TileMode.CLAMP);
this.getPaint().setShader(myShader);
a.recycle();
}
public void setCustomColor(int startColor,int endColor){
Shader myShader = new LinearGradient(0, 0, 0, 100,startColor, endColor, Shader.TileMode.CLAMP);
this.getPaint().setShader(myShader);
invalidate();
}
}
答案 1 :(得分:0)
要解决此问题,您需要确保检查设备上的网络连接,并确保在进行网络呼叫之前已建立有效的Internet连接。 发出网络请求之前检查Internet连接的方法 您可以像这样使用连接管理器
CamapaignActionField
这是从this answer中剔除而来的,您可以通过它检查网络的可用性。 如果您想更准确地监视网络是否处于活动状态,可以尝试在Google处对Checking network on Android DNS服务器执行ping操作。