- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 api,我可以从中获取信息,我想获取图像(如果有的话),然后将其放入 ListView 中。我得到了数据,但不明白如何获取图像,你能帮我吗?接口(interface)链接:http://gdetut.com/api/firms?salt=63926e380bdc96ef990d57898daeb71c&category_id=1普通 View 下的API:http://jsonprettyprint.com/json-pretty-printer.php
此应用程序显示服务站和汽车配件商店,需要从 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);
}
}
Activity 中的 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;
}
最佳答案
按照建议,您应该使用 picasso (为了进一步引用改造与 picasso、okhttp 和其他几个库同步工作)。
这样做的原因是,即使您将使用改造下载图像,您仍然需要将返回的数据转换为位图,以及缓存图像(如果您自己加载的图像多于少数)应用程序可能会因为没有正确处理你的内存使用而崩溃,幸运的是我们 picasso 自动这样做了)。
使用picasso使用gradle编译库:
compile 'com.squareup.picasso:picasso:2.5.2'
然后你就可以在任何你想要的地方使用它了,如下一个例子所示:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
我建议您查看我添加的链接,因为有更多选项可以添加到此代码(添加 View 持有者、调整大小等...)
关于Android Retrofit如何从api获取图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38269788/
我正在尝试迁移我的应用程序以使用 RxJava。我已经在使用 Retrofit,因此我正在尝试使用方法返回 Observables 的 Retrofit 接口(interface)。但是我现在在针对它
我想 post 数据如下: { "user_id":"14545646", "list":["4545645","4545645","4545645","4545645"]
我是改造新手。我向网站发出 POST 请求。网站以 HTML 形式返回响应。所以我会解析它。但是 Retrofit 尝试将其解析为 JSON。怎么办? @FormUrlEncoded @POST("/
我想异步执行 2 个网络调用 - 我正在使用 Retrofit+RxJava 来完成这个。这个逻辑来自一个简单的 Runner 类来测试解决方案。注意:这主要涉及服务器端的 RxJava。 我的代码如
我最近在改造的存储库中使用 Flow。 Result 的密封类 enum class ApiStatus{ SUCCESS, ERROR, LOADING } sealed c
我目前正在开发 Retrofit2 客户端的错误处理部分(使用 Retrofit 的 Rx 实现)。 找了一段时间,没有找到简单的方法将Retrofit返回的ResponseBody反序列化到我报错的
我从 retrofit 更新到 retrofit2 后出现此错误。 FATAL EXCEPTION: OkHttp Dispatcher Process: nz.co.datacom.mars.jun
在使用 RxJava 和 Retrofit 2 时,我正在尝试创建单元测试来覆盖我的应用何时收到特定响应。 我遇到的问题是,在 Retrofit 2 中,我看不到在不使用反射的情况下创建 retrof
在 Retrofit 1.9.x 中有一个 RetrofitError.Kind.NETWORK这让您可以轻松确定故障是否由网络错误引起。在新的 2.0 API 中,我们不再有 RetrofitErr
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
有没有办法用Refit动态输入参数? 我的 Refit 界面中有这段代码: [Get("/click?{parm}")] Task> SaveClick(string parm
我知道,Retrofit 在内部使用 OkHttp。但是,我可以看到一些开发人员提供了以下方法的应用 return new Retrofit.Builder() .baseUrl(Bu
在项目上安装这个库之后: compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0
在 Retrofit 1.x 中,我使用以下模式创建 API 服务类,该类模拟某些构建变体的不良网络连接。 // Retrofit 1 private T create(Class apiServi
Retrofit请求API格式(Android): @POST("getOrderStatus") @Headers("Content-Type:application/json") Obser
当我实例化 RestAdapter 时,我的应用程序总是崩溃 private void submitForm(SignupForm form){ RestAdapter adapter = n
我正要从 retrofit 1.9 迁移到最新版本并遇到问题... 我的设置: 三星 Galaxy S7 Edge(Android 7) 改造 2.3 OkHttp 3.8 迁移后突然遇到这个问题:
我正在尝试在 2.5.1-SNAPSHOT 中使用 retrofit 的协程支持,但我不断收到一个奇怪的异常。 我的改造服务类有: @GET("weather") suspend fun getFor
我正在尝试 Ktor通过转换一些当前正在使用的现有项目 Retrofit . 虽然我可以很容易地将请求转换为: client.get { url("$BASE_URL/something/so
使用改造 2,如何为上传的文件设置动态名称? 目前是这样的: @Part("avatar\"; filename=\"image\" ") RequestBody image, 但是,上传的文件名将是
我是一名优秀的程序员,十分优秀!