2016-07-08 9 views
1

私はapiを持っています。私は情報を取得して、画像を取得したい場合は、ListViewに入れます。データを取得しましたが、イメージを取得する方法を理解していない、私を助けることができますか? のAPIリンク:http://gdetut.com/api/firms?salt=63926e380bdc96ef990d57898daeb71c&category_id=1 アピ通常のビューで:http://jsonprettyprint.com/json-pretty-printer.phpAndroidの機能強化apiから画像を取得する方法

このアプリケーションは、彼らがしている場合、APIから画像をアップロードする必要があり、これを行うための最善の方法を、サービスステーション、自動車部品店を示して?

レトロフィットクラス:活動中

public class Retrofit { 

private static final String ENDPOINT = "http://gdetut.com/api"; 
private static ApiInterface apiInterface; 

interface ApiInterface { 
    @GET("/firms?salt=63926e380bdc96ef990d57898daeb71c&category_id=1") 
    void getPlaces(Callback<List<Places>> callback); 


} 

static { 
    init(); 
} 

private static void init() { 
    RestAdapter restAdapter = new RestAdapter.Builder() 
      .setEndpoint(ENDPOINT) 
      .setLogLevel(RestAdapter.LogLevel.FULL) 
      .build(); 
    apiInterface = restAdapter.create(ApiInterface.class); 
} 

public static void getPlaces (Callback<List<Places>> callback) { 
    apiInterface.getPlaces(callback); 
} 

} 

Ratrofit成功:

Retrofit.getPlaces(new Callback<List<Places>>() { 
     @Override 
     public void success(List<Places> places, Response response) { 


      listView.setAdapter(new MyAdapter(MainActivity.this, places)); 


     } 

アダプタ:

class MyAdapter extends ArrayAdapter<Places> { 

    public MyAdapter(Context context, List<Places> objects) { 
     super(context, R.layout.list_item, objects); 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     View rowView = convertView; 
     if (rowView == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      rowView = inflater.inflate(R.layout.list_item, parent, false); 
      holder = new ViewHolder(); 
      holder.nameOfPlace = (TextView) rowView.findViewById(R.id.name_id); 
      holder.subcategory_name = (TextView) rowView.findViewById(R.id.subcategory_name_id); 
      holder.geometryName = (TextView) rowView.findViewById(R.id.geometry_name_id); 
      holder.imageView = (ImageView) rowView.findViewById(R.id.image_id); 
      holder.rating = (TextView) rowView.findViewById(R.id.rating_id); 
      rowView.setTag(holder); 
     } else { 
      holder = (ViewHolder) rowView.getTag(); 
     } 

     Places places = getItem(position); 
     holder.nameOfPlace.setText(places.getName()); 
     holder.subcategory_name.setText(places.getSubcategory_name()); 
     holder.geometryName.setText(places.getGeometry_name()); 
     holder.imageView.setImageResource(places.getImage()); 
     holder.imageView.setImageResource(R.drawable.restaurant48); 
     holder.rating.setText(places.getRating()); 


     return rowView; 
    } 

    class ViewHolder { 

     public TextView nameOfPlace; 
     public TextView subcategory_name; 
     public TextView geometryName; 
     public TextView rating; 
     public ImageView imageView; 
    } 
} 

地クラス:

public class Places implements Serializable { 


String name; 
String geometry_name; 
String rating; 
String subcategory_name; 
int image; 

public Places(String name, String geometry_name, String rating,String subcategory_name, int image) { 
    this.name = name; 
    this.geometry_name = geometry_name; 
    this.rating = rating; 
    this.subcategory_name = subcategory_name; 
    this.image = image; 
} 



public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 


public String getGeometry_name() { 
    return geometry_name; 
} 

public void setGeometry_name(String geometry_name) { 
    this.geometry_name = geometry_name; 
} 

public String getRating() { 
    return rating; 
} 

public void setRating(String rating) { 
    this.rating = rating; 
} 

public String getSubcategory_name() { 
    return subcategory_name; 
} 

public void setSubcategory_name(String subcategory_name) { 
    this.subcategory_name = subcategory_name; 
} 

public int getImage() { 
    return image; 
} 

public void setImage(int image) { 
    this.image = image; 
} 

}

+0

[Retrofit APIでpng画像を取得する]のように見えます(http://stackoverflow.com/questions/25462523/retrofit-api-to -retrieve-a-png-image)である。 – Sufian

答えて

0

places.getImage()メソッドは、画像URLを返します。そしてストリームをデコードする必要がある時代のビューにurlを渡すことはできません。

またはUは、ピカソのライブラリを使用することができますが簡単にあなたがイメージリソースとして画像を設定しようとしている

+0

あなたのモデルクラスが間違っています。イメージは整数ではありません。それは配列です。 – LearnPainLess

+0

ピカソとのやり取りは? – Azarnoy

+0

まずは、画像URLを解析します。あなたのモデルクラスは正確ではありません。 – LearnPainLess

0

をageviewするURLを設定します

holder.imageView.setImageResource(places.getImage());

しかし、APIは、画像のURLを提供します。 Places内のデータタイプimageをStringに変更し、画像ロードライブラリで画像をリクエストする必要があります。私の個人的なお勧めはPicassoです。

+0

どのようにピカソでそれを行うには? – Azarnoy

+0

Stringでイメージを取得するにはどうすればよいですか? – Azarnoy

1

imageリソースでサーバーフィールドから取得しようとしたためにイメージを受信できませんが、jsonではこのフィールドがありません。いくつかの画像リンクはimages jsonフィールドにあります。 Btw、このクエリで画像(ビットマップ)を読み込むためにRetrofitを使用することはできませんが、今はリンクを受け取って別の方法で画像を読み込むことができます。したがって、他のライブラリ(GlideやPicassaなど)を使用することもできます。

+0

ピカソの使い方は? – Azarnoy

+0

あなただけの画像のURLを持つ文字列を受け取った後: 'Picasso.with(コンテキスト) .LOAD(URL) .into(holder.imageView)'本当のURLが含まれている 'url'は' STRING'ある (ましたあなたはデスクトップブラウザでも開くことができます) あなたのプロジェクトには、gradle依存関係としてPicassoライブラリを追加する必要があります。詳しくは、http://square.github.io/picasso/ –

+0

を参照してください。 URL? – Azarnoy

1

picassoを使用することをお勧めします(picasso、okhttpなどいくつかのライブラリと同期して、後で追加作業を行うことができます)。

その理由は、改造を使用してイメージをダウンロードしても、イメージをキャッシュするだけでなく、ビットマップに戻すデータを変換する必要があるからです。あなたのアプリはおそらくメモリ使用量が適切に処理されていないためにクラッシュするでしょう。

ピカソのGradleを使用してライブラリをコンパイル使用する:

コンパイル「com.squareup.picasso:ピカソ:2.5。あなたが好きな場所次の例で述べたように2'

を、あなたはそれを使用することができます。

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 

私はより多くの選択肢があるので、私は追加リンクの上に行くためにかかわらず、あなたをお勧めしますこのコードに追加するには(ビューホルダーの追加、サイズ変更など)

+0

私はそれを望ましくありませんが、私はどのようにそのjsonからのイメージのURLを得ることができますか? – Azarnoy

+0

ありがとうございます。最近Refrofitをプラグインし、使用する画像ライブラリを選択しました:フレスコ画、グライドまたはPicasso。 – CoolMind

+0

Picasso + Retrofitの簡単な例を紹介します(旧バージョン):http://themakeinfo.com/2015/04/android-retrofit-images-tutorial/ – CoolMind

関連する問題