小编Meh*_*aji的帖子

当我们有OkHttp时,为什么要使用Retrofit

使用OkHttp我们可以发出HTTP请求然后从服务器获得响应

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
  .url(url)
  .build();
Response response = client.newCall(request).execute();
Run Code Online (Sandbox Code Playgroud)

然后用Gson lib将响应转换为我们需要的对象.

这是来自Square/OkHttp doc:

它的请求/响应API设计具有流畅的构建器和不变性.它支持同步阻塞调用和带回调的异步调用

我从stackOverFlow中读取

如果可用,Retrofit会自动使用OkHTTP

.

所以我的问题是究竟是什么改造?

什么改造可以做到OkHttp不能?!

我认为OkHttp和Gson解决了请求API问题,那么Retrofit为我们解决了什么问题?

java android retrofit okhttp

89
推荐指数
4
解决办法
4万
查看次数

如何在Android版的Google+应用中为FloatingActionButton制作动画?

我将FloatingActionButton设置为屏幕底部,我想为按钮设置动画.

  • 向下滚动时隐藏
  • 向上滚动时显示

就像谷歌在他们的Google+应用中实现它一样.

我认为需要CoordinatorLayout和AppBarLayout但是如何实现它以将它与FloatingActionButton一起使用?

android android-animation material-design floating-action-button android-design-library

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

UIStackView 内容拥抱在 UITableViewCell 中无法按预期工作

我有两个垂直StackView嵌套在水平StackView.
对于第一个:

titlesView.axis = .vertical
titlesView.distribution = .fill
titlesView.alignment = .leading
Run Code Online (Sandbox Code Playgroud)

第二个:

checkView.axis = .vertical
checkView.distribution = .fil
checkView.alignment = .center
Run Code Online (Sandbox Code Playgroud)

和 holderStackView(root)

    holderStackView.axis = .horizontal
    holderStackView.alignment = .center
    holderStackView.distribution = .fill


    holderStackView.addArrangedSubview(titlesView)
    holderStackView.addArrangedSubview(checkView)


    titlesView.setContentHuggingPriority(.defaultLow, for: .horizontal)
    checkView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
Run Code Online (Sandbox Code Playgroud)

我希望 titlesView 必须填充 StackView,但 checkView StackView 填充 holderStackView 堆栈视图。

如果我为它们center或设置对齐leading,那么它会按预期工作并且第一个会增长。



期待:

在此处输入图片说明



现实:

在此处输入图片说明



我该如何解决这个问题?

它仅发生在 的contentViewUITableViewCell,请参阅下面的最小示例:

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.separatorColor = UIColor(red:0.87, green:0.87, …
Run Code Online (Sandbox Code Playgroud)

uitableview ios autolayout swift uistackview

8
推荐指数
1
解决办法
6895
查看次数

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

Retrfoit:使用@Multipart将jsonObject作为@RequestBody发送

我想通过Retrofit将json对象发送到服务器作为RequestBody

 {"attach": {
    "image": {
        "height": 1473,
        "urlRef": "",
        "width": 1473
    },
    "video": {
        "duration": "4.365",
        "height": 1920,
        "thumbUrl": "",
        "urlRef": "",
        "width": 1080
    }
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的改造对象

  Retrofit.Builder retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create());
Run Code Online (Sandbox Code Playgroud)

这是我的改造界面:

@Multipart
@POST("post/")
Observable<Response> postAttach(
        @Part("attach") RequestBody asset
        , @Part MultipartBody.Part attachment
);
Run Code Online (Sandbox Code Playgroud)

并创建RequestBody如下:

RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), gson.toJson(myAttach));
Run Code Online (Sandbox Code Playgroud)

但是这个请求发送json作为字符串而不是json对象

那我怎么发送jsonObejct?

注意:如果我使用@Body发送作为json对象,但我不能将@Body与@MultiPart一起使用!

java android retrofit2 okhttp3

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

recyclerView 的 setOnClickListener 不起作用

因为RecyclerView如果它没有任何项目,则单击RecyclerView有效,但如果它有项目,则单击RecyclerView不起作用。小心我的意思是点击 just RecyclerViewnotRecyclerView的项目

recyclerView.setOnClickListener(view -> {Timber.d("recyclerView clicked");});
Run Code Online (Sandbox Code Playgroud)

RecyclerView即使上面有项目,我如何设置可点击。

android view android-layout android-view android-recyclerview

4
推荐指数
3
解决办法
4232
查看次数