本文介绍了Android Retrofit 不显示错误,也不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的改造声明不起作用我在build.gradel中实现了适当的模块.几天来,代码运行良好,突然停止工作,也没有出现任何错误.
Below Retrofit declaration is not working i have implemented proper modules in build.gradel. For few days the code was working fine, suddenly it stopped working and not giving any error also.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(MyImageInterface.IMAGEURL)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
public interface MyImageInterface {``
String IMAGEURL = connect.cons+"book_insert.php";
@FormUrlEncoded
@POST("book_insert.php")
Call<String> getImageData(
@Field("name") String name,
@Field("image") String image
);
}
I have applied following solution for retrofit connection as suggested
public class ApiClient {
private static Retrofit retrofit = null;
static final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(40, TimeUnit.SECONDS)
.writeTimeout(50, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();
public static Retrofit getApiClient(){
try {
if (retrofit == null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(MyImageInterface.IMAGEURL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient)
.build();
}
}catch(Exception e)
{
Log.v("retroerror",e.getMessage());
}
return retrofit;
}
}
但是给了我错误
"baseUrl 必须以/:
结尾http://13387c25fbd0.ngrok.io/my1/book_insert.php"
推荐答案
对于 Connection 试试这个
public class ApiClient {
private static Retrofit retrofit = null;
static final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
public static Retrofit getApiClient(){
if(retrofit==null){
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(base_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient)
.build();
}
return retrofit;
}
}
您可以像这样创建与接口的连接,然后您可以通过接口函数将调用排入队列.
You can create your connection with your interface like this, then you to enqueue your call by interface funtions.
ApiClient.getApiClient().create(ApiInterface.class)
这篇关于Android Retrofit 不显示错误,也不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!