【问题标题】:Android Retrofit not showing error and not working alsoAndroid Retrofit 不显示错误,也不工作
【发布时间】:2021-03-22 17:06:08
【问题描述】:

下面的改造声明不起作用我已经在 build.gradel 中实现了适当的模块。这几天代码运行良好,突然停止运行,也没有出现任何错误。

 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"

【问题讨论】:

  • 发布你使用 Retrofit 调用的代码

标签: php android image upload retrofit


【解决方案1】:

对于连接试试这个

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;
    }
}

您可以像这样创建与您的界面的连接,然后您可以通过界面功能将您的调用排入队列。

ApiClient.getApiClient().create(ApiInterface.class)

【讨论】:

  • 如果您想了解更多详情,请咨询我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 2017-04-30
相关资源
最近更新 更多