使用glide从服务器加载图像

时间:2019-10-17 23:56:59

标签: java android android-studio recycler-adapter glide

//网站链接

private String imageUrls[]= {
        "https://dicodingacademy.blob.core.windows.net/eventimages/dsc_chapter_stmik_stikom_indonesia_introducing_developer_student_club_and_google_technologies_logo_041019113915.png",
        "https://dicodingacademy.blob.core.windows.net/eventimages/dsc_ueu_event_workshop_4_flutter_aej_build_your_first_app_with_flutter_fetching_data_from_an_api_logo_151019182429.JPG",
        "https://dicodingacademy.blob.core.windows.net/eventimages/build_your_own_startup_for_a_better_generation_logo_270919142930.png",
        "https://dicodingacademy.blob.core.windows.net/eventimages/fundamental_unity_engine_logo_280919221953.png",
        "https://dicodingacademy.blob.core.windows.net/eventimages/colosseum_2019_x_dsc_ithb_ui_ux_design_mobile_app_prototyping_with_figma_logo_141019071945.png",
        "https://dicodingacademy.blob.core.windows.net/eventimages/make_unit_testing_great_again_logo_141019104313.jpg",
};

public RecyclerViewAdapter(Context context, ArrayList list) {
    this.context = context;
    this.mData = list;
}

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView tvNama, tvSource, tvSummary;
    public ImageView img;


    public ViewHolder(View v) {
        super(v);
        tvNama = v.findViewById(R.id.name);
        tvSource = v.findViewById(R.id.oleh);
        tvSummary = v.findViewById(R.id.summary);
        img = v.findViewById(R.id.img1);

    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main_list, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}
// this is my viewHolder
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.tvNama.setText("Nama : " + mData.get(position).getName());
    holder.tvSource.setText("" + mData.get(position).getOwner_name());
    holder.tvSummary.setText("" + mData.get(position).getSummary());

    //i don't know how to edit this
Glide
            .with(context)
            .load(mData.get(position).getImage())
            .into(holder.img);
}

@Override
public int getItemCount() {
    return mData.size();
}

请帮助,我想从该URL加载图像,但它总是强制关闭。我不知道该怎么做。

  • 我已经搜索了stackoverflow,google,youtube等,但我还是不明白
  • 我是编程新手

Logcats:

10-18 07:08:04.888 28578-28578/com.example.projectbesar E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.projectbesar, PID: 28578
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectbesar/com.example.projectbesar.MainActivity}: java.lang.IllegalArgumentException: Unable to create converter for class com.example.projectbesar.ModelBiodata
        for method ApiRetrofitInterface.getData
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2750)
        at android.app.ActivityThread.access$1100(ActivityThread.java:186)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1606)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:238)
        at android.app.ActivityThread.main(ActivityThread.java:6006)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)
     Caused by: java.lang.IllegalArgumentException: Unable to create converter for class com.example.projectbesar.ModelBiodata
        for method ApiRetrofitInterface.getData
        at retrofit2.Utils.methodError(Utils.java:52)
        at retrofit2.HttpServiceMethod.createResponseConverter(HttpServiceMethod.java:115)
        at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:82)
        at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:37)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:149)
        at java.lang.reflect.Proxy.invoke(Proxy.java:393)
        at $Proxy0.getData(Unknown Source)
        at com.example.projectbesar.MainActivityPresenter.getData(MainActivityPresenter.java:46)
        at com.example.projectbesar.MainActivity.onCreate(MainActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:6466)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637)
            ... 9 more
     Caused by: java.lang.SecurityException: Can't make method constructor accessible
        at java.lang.reflect.Constructor.setAccessible(Constructor.java:336)
        at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:101)
        at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:83)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:99)
        at com.google.gson.Gson.getAdapter(Gson.java:423)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
        at com.google.gson.Gson.getAdapter(Gson.java:423)
        at com.google.gson.internal.bind.ArrayTypeAdapter$1.create(ArrayTypeAdapter.java:48)
        at com.google.gson.Gson.getAdapter(Gson.java:423)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
        at com.google.gson.Gson.getAdapter(Gson.java:423)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
        at com.google.gson.Gson.getAdapter(Gson.java:423)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory

这是我的模型课,我认为有一些东西。 模型:     包com.example.projectbesar;

import java.util.ArrayList;

public class ModelBiodata {
        private String success;
        private ArrayList<Data> data;
    }

    public String getSuccess() {
            return success;
        }

        public void setSuccess(String success) {
            this.success = success;
        }

        public ArrayList<Data> getData() {
            return data;
        }

        public void setData(ArrayList<Data> data) {
            this.data = data;
        }
}

1 个答案:

答案 0 :(得分:0)

好像您没有用于在ModelBiodata类中设置变量的构造函数。

您可以考虑如下修改类。

public class ModelBiodata {
    private String success;
    private ArrayList<Data> data;

    public ModelBioData() {
    }

    public void setSuccess(String success) {
        this.success = success;
    }

    public void setData(ArrayList<Data> data) {
        this.data = data;
    }

    public String getSuccess() {
        return success;
    }

    public void setSuccess(String success) {
        this.success = success;
    }

    public ArrayList<Data> getData() {
        return data;
    }

    public void setData(ArrayList<Data> data) {
        this.data = data;
    }
}

如果Data类没有任何public构造函数,则也对该类进行相同的操作,并添加设置器以在该类中初始化私有变量。

希望能解决您的问题。