我正在使用Retrofit访问RESTful api.基本网址是:
这是界面的代码:
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 }
这里发生了什么?
可以使用OkHttp拦截器指定需要添加到每个请求的标头.
可以使用以前的版本轻松完成,这是相关的QA.
但是使用改造2,我找不到可以应用于对象的类似物setRequestInterceptor或setInterceptor方法Retrofit.Builder.
此外,它似乎没有RequestInterceptor在OkHttp了.Retrofit的doc将我们引用到Interceptor,我不太明白如何将它用于此目的.
我怎样才能做到这一点?
我正在使用以下代码行为使用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
这里有什么问题?
如何为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) 我试图填补一些数据到一个RecyclerView与GridLayoutManager:
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
我需要发送带头的请求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.
我试图将一些自定义字体应用于我的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
我想实现这样的事情。这样我就可以控制它的颜色,条数,速度等。
以下是显示在Google Play音乐应用中当前正在播放的项目旁边的动画。
我将以类似的方式进行播放:在音乐应用程序中当前正在播放的歌曲旁边。
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 做到这一点吗?
从AppCompatButton参考页面:
当您在布局中使用 Button 时,这将自动使用。您应该只需要在编写自定义视图时手动使用此类。
我将法线投射Button到AppCompatButton,以便我可以使用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
有任何想法吗?