iT邦幫忙

0

Android使用Retrofit API抓取資料的問題

大家好,我最近在做Android上使用Retrofit API抓取Json時,遇到底下這個問題:
https://ithelp.ithome.com.tw/upload/images/20211012/20140126zU8hgZbEb4.jpg
Id_CommodityImage的Type是GUID,那我要怎麼把他顯示出來?
我用Postman是這樣:
https://ithelp.ithome.com.tw/upload/images/20211012/20140126KtFGtr1rtH.jpg
我是使用Retrofit API抓取,並且使用RecyclerView來做顯示
以下附上我的程式碼:
activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

recyclerview_textview.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/title2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="TextView"
        android:textSize="30sp" />
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/iv"
        android:padding="30dp"
        android:text="ImageView"
        android:scaleType="centerInside"
        />
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="TextView"
        android:textSize="30sp" />

</LinearLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    RecyclerAdapter adapter;
    private ArrayList<String> mitem=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://10.25.7.167:62678/")  //連接你要的uri
                .addConverterFactory(GsonConverterFactory.create())  //gson解析
                .build();
        ApiGet apiGet =retrofit.create(ApiGet.class);
        Call<List<DataItem>> call =apiGet.getPosts();  //呼叫call連線服務

        call.enqueue(new Callback<List<DataItem>>() {  //透過 Callback 來等待回傳成功或失敗的資料
            @Override
            public void onResponse(Call<List<DataItem>> call, Response<List<DataItem>> response) {
                for (DataItem item : response.body()) {
                    Log.d( "API","name: " + item.getPrice());
                    Log.d("API", "url: " + item.getName_Classification());
                    //Log.d( "API","name: " + item.getId_CommodityImage());
                    RecyclerV();
                    //mitem.add(String.valueOf(item.getId_CommodityImage()));
                    mitem.add(String.valueOf(item.getPrice()));
                    mitem.add(item.getName_Classification());
                    mitem.add(String.valueOf(item.getId_SubClassification()));
                    mitem.add(item.getName_SubClassification());
                    mitem.add(String.valueOf(item.getId_CommodityDetail()));
                    mitem.add(String.valueOf(item.getId_Classification()));
                }
            }

            @Override
            public void onFailure(Call<List<DataItem>> call, Throwable t) {
                Log.d("API", "fail:"+t.toString());
            }
        });
    }
    public class DataItem {
        private Float Price;
        private Integer Id_CommodityDetail;
        private Integer Id_Classification;
        private Integer Id_SubClassification;
        //        @SerializedName("body")//@SerializedName註解可以讓String text得到body的值
        private String Name_Classification;
        private String Name_SubClassification;
        //private byte[] Id_CommodityImage;

        //public byte[] getId_CommodityImage(){
        //    return Id_CommodityImage;
        //}

        public Float getPrice() {
            return Price;
        }

        public Integer getId_SubClassification() {
            return Id_SubClassification;
        }

        public Integer getId_CommodityDetail() {
            return Id_CommodityDetail;
        }

        public Integer getId_Classification() {
            return Id_Classification;
        }

        public String getName_Classification() {
            return Name_Classification;
        }

        public String getName_SubClassification() {
            return Name_SubClassification;
        }
    }
    public interface ApiGet {
        @GET("api/Commodity/GetNew?topNumber=5")
        Call<List<DataItem>> getPosts();
    }
    //設置RecyclerView
    private void RecyclerV() {
        RecyclerView recycle =findViewById(R.id.recycle);
        recycle.setLayoutManager(new LinearLayoutManager(this));
        recycle.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter=new RecyclerAdapter(this,mitem);
        recycle.setAdapter(adapter);
    }
    /*
    public static Bitmap decodeByteArray(byte[] data, int offset, int length){

    }
    */
}

RecyclerAdapter.java:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private List<String> mitem;
    private LayoutInflater inflater;
    String[] field = {"價格","種類","不知道","介紹","類別"};

    RecyclerAdapter(Context context, List<String> item){
        this.inflater=LayoutInflater.from(context);
        this.mitem=item;
    }
    //取得控制元件
    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title,title2;
        ImageView iv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title=itemView.findViewById(R.id.title);
            title2=itemView.findViewById(R.id.title2);
            iv=itemView.findViewById(R.id.iv);
        }
    }
    //連接recyclerview_textview.xml
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view =inflater.inflate(R.layout.recyclerview_textview,parent,false);
        return new ViewHolder(view);
    }
    //連結剛才在ViewHolder宣告過的元件,就可以從mitem中取出你要的值
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if(position < field.length) {
            holder.title2.setText(field[position]);
            holder.title.setText(mitem.get(position));
            //holder.iv.setImageResource(parseInt(mitem.get(position)));
        }
    }
    //取得顯示數量的大小,return 陣列長度
    @Override
    public int getItemCount() {
        return mitem.size();
    }
}

註解的部分是我試過失敗的,請各位大大幫忙小弟解答。

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0

GUID 應該是對應圖片的識別碼
要去找對應的圖片檔

請問那我要怎麼找到對應的圖片檔,伺服器是C#,資料庫是使用MS SQL Server

這要問你們公司對應圖片怎麼抓
或是去尋找這字符串是如何被創建的

0

可以參考這篇:
https://www.cnblogs.com/jilodream/p/12567447.html

先測看看 postman 有沒有辦法抓到圖片檔

我要發表回答

立即登入回答