毕加索无法从s3桶中加载图像

时间:2017-03-09 11:05:50

标签: android amazon-s3 picasso

我把图像放在s3bucket上的aws上。但不知何故,毕加索无法从s3桶下载图像。

错误stacktarce如下所示:

03-09 15:43:45.503 9620-9620/demand.inn.com.quflip.production W/System.err: java.net.UnknownHostException: Unable to resolve host "s3.amazonaws.com": No address associated with hostname
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:427)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500)
03-09 15:43:45.504 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.squareup.picasso.UrlConnectionDownloader.load(UrlConnectionDownloader.java:93)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.squareup.picasso.NetworkRequestHandler.load(NetworkRequestHandler.java:47)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:206)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:159)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-09 15:43:45.505 9620-9620/demand.inn.com.quflip.production W/System.err:     at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:411)

PopularItemsAdapter.java

public class PopularItemsAdapter extends RecyclerView.Adapter<PopularItemsAdapter.MyViewHolder> {

    public interface OnItemClickListener {
        void onItemClick();
    }

    private Context mContext;
    private List<FoodTruckItem> foodTruckItemList;
    private OnItemClickListener listener;

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView itemTag, itemName, foodPrice, addText;
        public ImageView itemImage;

        public MyViewHolder(View view) {
            super(view);
            itemTag = (TextView) view.findViewById(R.id.itemTag);
            itemImage = (ImageView) view.findViewById(R.id.itemImage);
            itemName = (TextView) view.findViewById(R.id.itemName);
            foodPrice = (TextView) view.findViewById(R.id.foodPrice);
            addText = (TextView) view.findViewById(R.id.addText);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            //passing the clicked position to the parent class
            listener.onItemClick();
        }
    }


    public PopularItemsAdapter(Context mContext, List<FoodTruckItem> foodTruckItemList, OnItemClickListener listener) {
        this.mContext = mContext;
        this.foodTruckItemList = foodTruckItemList;
        this.listener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view_popular_items, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        FoodTruckItem foodTruckItem = foodTruckItemList.get(position);
        holder.itemName.setText(foodTruckItem.getItemName());
//        holder.itemTag.setText(foodTruckItem.getSpecialTag());
        holder.foodPrice.setText("$" + String.valueOf(foodTruckItem.getItemPrice()));
//        holder.itemImage.setImageResource(R.drawable.image_food);

//        Picasso.with(mContext)
//                .load(foodTruckItem.getItemImg())
//                .into(holder.itemImage, new com.squareup.picasso.Callback() {
//                    @Override
//                    public void onSuccess() {
//                        Toast.makeText(mContext, "loaded", Toast.LENGTH_LONG).show();
//
//                    }
//
//                    @Override
//                    public void onError() {
//                        Toast.makeText(mContext, "not loaded", Toast.LENGTH_LONG).show();
//                    }
//                });
        Picasso.Builder builder = new Picasso.Builder(mContext);
        builder.listener(new Picasso.Listener() {
            @Override
            public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                exception.printStackTrace();
            }
        });
        builder.build().load(foodTruckItem.getItemImg()).into(holder.itemImage);
        setFontStyle(holder);
    }


    private void setFontStyle(MyViewHolder holder) {
        holder.itemName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), Consts.montserrat_bold));
        holder.itemTag.setTypeface(Typeface.createFromAsset(mContext.getAssets(), Consts.montserrat_bold));
        holder.foodPrice.setTypeface(Typeface.createFromAsset(mContext.getAssets(), Consts.montserrat_bold));
        holder.addText.setTypeface(Typeface.createFromAsset(mContext.getAssets(), Consts.montserrat_bold));
    }


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

PopularItemsFragment.java

public class PopularItemsFragment extends Fragment implements PopularItemsAdapter.OnItemClickListener {

    private RecyclerView recyclerView;
    private PopularItemsAdapter popularItemsAdapter;
    private List<FoodTruckItem> foodTruckItemList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_popular_items, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        popularItemsAdapter = new PopularItemsAdapter(getActivity(), foodTruckItemList, this);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(popularItemsAdapter);

        if (InternetConnection.checkConnection(getActivity())) {
            //If the internet is working then only make the request
            Observable<GenericResponse<List<FoodTruckItem>>> results = RetroClient.getApiService().getItemList();
            results.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<GenericResponse<List<FoodTruckItem>>>() {
                        @Override
                        public void onCompleted() {
                            unsubscribe();
                        }

                        @Override
                        public void onError(Throwable e) {
                        }

                        @Override
                        public void onNext(GenericResponse<List<FoodTruckItem>> response) {
                            foodTruckItemList.addAll(response.getData());
                            popularItemsAdapter.notifyDataSetChanged();
                        }
                    });
        } else {
            Snackbar.make(view, R.string.string_internet_connection_warning, Snackbar.LENGTH_INDEFINITE).show();
        }

        return view;
    }


    @Override
    public void onItemClick() {

    }
}

如果我将s3bucket网址粘贴到浏览器中,则会成功加载图片。但不知何故,picaso无法识别它。还有其他方法吗?

0 个答案:

没有答案