小编Ash*_*lak的帖子

Retrofit 2从主URL删除主机名后的字符

我正在使用Retrofit访问RESTful api.基本网址是:

http://api.example.com/service

这是界面的代码:

public interface ExampleService {
    @Headers("Accept: Application/JSON")
    @POST("/album/featured-albums")
    Call<List<Album>> listFeaturedAlbums();
}
Run Code Online (Sandbox Code Playgroud)

这就是我发送请求并接收响应的方式:

new AsyncTask<Void, Void, Response<List<Album>>>() {

        @Override
        protected Response<List<Album>> doInBackground(Void... params) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://api.example.com/service")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            ExampleService service = retrofit.create(ExampleService.class);

            try {
                return service.listFeaturedAlbums().execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Response<List<Album>> listCall) {
            Log.v("Example", listCall.raw().toString());
        }
    }.execute();
Run Code Online (Sandbox Code Playgroud)

我得到的日志是奇怪的事情:

V /示例:响应{protocol = http/1.1,代码= 404,消息=未找到,url = http://api.example.com/album/featured-albums }

这里发生了什么?

android retrofit

113
推荐指数
1
解决办法
2万
查看次数

使用Retrofit 2为所有请求添加标头

Retrofit 2的文档说:

可以使用OkHttp拦截器指定需要添加到每个请求的标头.

可以使用以前的版本轻松完成,这是相关的QA.

但是使用改造2,我找不到可以应用于对象的类似物setRequestInterceptorsetInterceptor方法Retrofit.Builder.

此外,它似乎没有RequestInterceptorOkHttp了.Retrofit的doc将我们引用到Interceptor,我不太明白如何将它用于此目的.

我怎样才能做到这一点?

java android header httprequest retrofit

112
推荐指数
6
解决办法
8万
查看次数

升级到OkHttp3后,OkHttpClient抛出异常

我正在使用以下代码行为使用Retrofit2发送的所有请求添加默认标头:

private static OkHttpClient defaultHttpClient = new OkHttpClient();
static {
    defaultHttpClient.networkInterceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request().newBuilder()
                    .addHeader("Accept", "Application/JSON").build();
            return chain.proceed(request);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在将改造升级到beta-3版本之后,我还必须将OkHttp升级到OkHttp3(实际上我只是将包名称从okhttp更改为okhttp3,该库包含在改造中).之后我从这一行得到例外:

defaultHttpClient.networkInterceptors().add(new Interceptor());
Run Code Online (Sandbox Code Playgroud)

引起:java.util.Collections的java.lang.UnsupportedOperationException $ UnmodifiableCollection.add(Collections.java:932)


引起:java.lang.ExceptionInInitializerError


这里有什么问题?

android retrofit okhttp retrofit2 okhttp3

31
推荐指数
1
解决办法
1万
查看次数

使用Retrofit 2重试请求

如何为Retrofit 2库发送的请求添加重试功能.就像是:

service.listItems().enqueue(new Callback<List<Item>>() {
        @Override
        public void onResponse(Response<List<Item>> response) {
            ...
        }

        @Override
        public void onFailure(Throwable t) {
            ...
        }
    }).retryOnFailure(5 /* times */);
Run Code Online (Sandbox Code Playgroud)

java android retrofit

27
推荐指数
3
解决办法
2万
查看次数

如何从右到左填充RecyclerView和GridLayoutManager

我试图填补一些数据到一个RecyclerViewGridLayoutManager:

GridLayoutManager layoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
Run Code Online (Sandbox Code Playgroud)

这将从左到右将数据填充到网格中.第一项将被放入左上角等地方.

但我正在开发的应用程序是专为RTL语言设计的,所以我需要从右到左填充它.

我试图改变最后一个参数true.但它只是RecyclerView一直向下滚动.

设置layoutDirection属性RecyclerView也不起作用(不考虑我的min API为16,并且为API17 +添加了此属性):

<android.support.v7.widget.RecyclerView
        android:id="@+id/grid"
        android:layoutDirection="rtl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)

如何使用RecyclerView?创建一个右到左填充网格?

android right-to-left gridlayoutmanager android-recyclerview

27
推荐指数
3
解决办法
5703
查看次数

Retrofit 2:@FormUrlEncoded with default fields

我需要发送带头的请求application/x-www-form-urlencoded.响应是JSON格式的一些音乐专辑的列表.可以有两个可选参数:total(默认值= 5)和begin(默认值= 0)

这是我用来发送此请求的接口:

public interface MusicService {
    @Headers("Accept: Application/JSON")
    @FormUrlEncoded
    @POST("album/featured-albums")
    Call<List<Album>> listFeaturedAlbums(@Field("total") int total, @Field("begin") int begin);
}
Run Code Online (Sandbox Code Playgroud)

问题是,如何将默认值设置为这些字段中的一个或两个,这样我就不必在每个请求中发送参数.例如,我想在每个请求中获得30个项目,并且只使用begin字段.或者我想使用两个字段的默认值:

public interface MusicService {
    @Headers("Accept: Application/JSON")
    @FormUrlEncoded
    @POST("album/featured-albums")
    Call<List<Album>> listFeaturedAlbums();
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我收到一个错误:

java.lang.IllegalArgumentException:表单编码方法必须至少包含一个@Field.

java android retrofit

14
推荐指数
1
解决办法
5455
查看次数

颜色旁边的数字是什么意思?

我正在读这篇关于材料设计的文章.在颜色列表中,每种颜色旁边都有一个数字,当颜色值增加时,颜色似乎会变暗.

在此输入图像描述

这个数字更准确地说是什么意思?

编辑:由于所有答案都是关于十六进制值,我正在添加此编辑以澄清问题.我的问题是左手边的数字,如700,500,...不是十六进制数字(#3f51b5,...)

编辑2:在RGB模型中,红色,绿色或蓝色中的每一个都可以具有0到255的标度值.0表示缺少颜色,255表示颜色以全功率存在.左侧数字是否有数字含义?我可以计算一个颜色的'700',假设它的'500'是#3F51B5吗?或者这些数字只是调色板中不同色调的名称?

android tint colors material-design

9
推荐指数
2
解决办法
1646
查看次数

使用Android-M数据绑定应用单行字体不起作用

我试图将一些自定义字体应用于我的TextView一行,如Lisa Wray的帖子所述.这TextView是一个项目的一部分RecyclerView

我已将数据绑定依赖项添加到我的顶级构建文件中.

classpath 'com.android.tools.build:gradle:1.3.0'
classpath "com.android.databinding:dataBinder:1.0-rc1"
Run Code Online (Sandbox Code Playgroud)

我也将插件应用到我的主模块:

apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
Run Code Online (Sandbox Code Playgroud)

这是item.xml将添加到的文件RecyclerView.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/tools">

    <data></data>

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="100dp"
        android:layout_height="130dp"
        android:layout_gravity="center"
        card_view:cardCornerRadius="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="100dp"/>

            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:font="@{@string/font_yekan}"/>

        </LinearLayout>
    </android.support.v7.widget.CardView>
</layout>
Run Code Online (Sandbox Code Playgroud)

我添加了一个layout根元素并app:font="@{@string/font_yekan}"结合了一个静态setter方法:

@BindingAdapter({"bind:font"})
public static void setFont(TextView textView, String fontName) {
    textView.setTypeface(Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName));
}
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩.但是当我运行程序时,字体不会改变.但是,当我删除上面的静态方法时,我收到以下错误:

找不到参数类型为java.lang.String的属性'app:font'的setter.

所以数据绑定框架已经识别出绑定的东西,但是setter方法没有被调用(Logs不打印输出).

这里有什么问题?

java data-binding android android-databinding android-6.0-marshmallow

5
推荐指数
1
解决办法
1923
查看次数

实施类似Google-Play-Music的动画(播放列表项旁边的3条形图)

我想实现这样的事情。这样我就可以控制它的颜色,条数,速度等。

以下是显示在Google Play音乐应用中当前正在播放的项目旁边的动画。

我将以类似的方式进行播放:在音乐应用程序中当前正在播放的歌曲旁边。

在此处输入图片说明

animation android

5
推荐指数
1
解决办法
4193
查看次数

在 Android Studio 中生成 json_serialized(Flutter/Dart 插件)样板代码的快捷方式

Dart 的json_serialized插件,在自动生成一些容易出错且繁琐的代码部分方面做得很好,以换取一些样板文件:两个方法、一个注释和一个对生成文件的引用。

import 'package:json_annotation/json_annotation.dart';

part 'location.g.dart';

@JsonSerializable()
class Location {
  final double lat;
  final double lng;

  Location(this.lat, this.lng);

  factory Location.fromJson(Map<String, dynamic> json) =>
       _$LocationFromJson(json);

  Map<String, dynamic> toJson() => _$LocationToJson(this);
}
Run Code Online (Sandbox Code Playgroud)

显然,这也最好由机器完成,就像此类的构造函数一样:我只需编写最终字段,然后按 alt+enter 键,Android Studio 就会为我放置构造函数。

有人知道如何让 Android Studio 为 json_serialized 做到这一点吗?

auto-generate dart android-studio flutter json-serializable

5
推荐指数
1
解决办法
6442
查看次数

意外转换为 AppCompatButton:布局标记为 Button

AppCompatButton参考页面:

当您在布局中使用 Button 时,这将自动使用。您应该只需要在编写自定义视图时手动使用此类。

我将法线投射ButtonAppCompatButton,以便我可以使用setSupportBackgroundTintList方法:

AppCompatButton button = (AppCompatButton) findViewById(R.id.normalButton);
button.setSupportBackgroundTintList(ColorStateList.valueOf(tintColor));
Run Code Online (Sandbox Code Playgroud)

它的构建和运行没有任何问题,但 Android Studio 1.4 在投射线上给了我恼人的红色高光:

意外转换为 AppCompatButton:布局标记为 Button

有任何想法吗?

android casting lint android-appcompat android-studio

2
推荐指数
1
解决办法
4154
查看次数