小编Ear*_*tos的帖子

无法在Android Retrofit库中为我的班级创建转换器

我从使用Volley迁移到Retrofit,我已经拥有了我之前使用的gson类,用于将JSONObject响应转换为实现gson注释的对象.当我尝试使用改造来制作http get请求但我的应用程序崩溃时出现此错误:

 Unable to start activity ComponentInfo{com.lightbulb.pawesome/com.example.sample.retrofit.SampleActivity}: java.lang.IllegalArgumentException: Unable to create converter for class com.lightbulb.pawesome.model.Pet
    for method GitHubService.getResponse
Run Code Online (Sandbox Code Playgroud)

我遵循改造网站的指南,我想出了这些实现:

这是我尝试执行复古http请求的活动:

public class SampleActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("**sample base url here**")
                .build();

        GitHubService service = retrofit.create(GitHubService.class);
        Call<Pet> callPet = service.getResponse("41", "40");
        callPet.enqueue(new Callback<Pet>() {
            @Override
            public void onResponse(Response<Pet> response) {
                Log.i("Response", response.toString());
            }

            @Override
            public void onFailure(Throwable t) {
                Log.i("Failure", t.toString());
            }
        });
        try{
            callPet.execute();
        } …
Run Code Online (Sandbox Code Playgroud)

android gson retrofit

118
推荐指数
7
解决办法
7万
查看次数

如何使用Android中的Volley库上传文件?

我已经有一个Request子类用于http发送到服务器.问题是,我不知道如何为文件添加参数.将字符串发布到服务器很容易.但我需要将文件添加为不同的参数.我该怎么做?

public class AddNewPetRequest extends Request<JSONObject> {

    private Response.Listener<JSONObject> listener;

    public AddNewPetRequest(String url, Map<String, String> params,
                                Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
        super(Request.Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    public AddNewPetRequest(int method, String url, Map<String, String> params,
                                Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return params;
    };

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new …
Run Code Online (Sandbox Code Playgroud)

android file http-post httprequest android-volley

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

滚动没有内容时如何禁用CollapsingToolbar崩溃?

我正在使用android支持设计'com.android.support:design:22.2.1'我的问题是当滚动没有内容时,折叠工具栏仍然启用折叠操作.如果滚动中没有内容,我需要删除视图的折叠.

我的XML:

  <android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:background="@android:color/white"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
                    app:expandedTitleMarginEnd="64dp"
                    app:expandedTitleMarginStart="48dp"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <fragment
                        android:id="@+id/pawfile_header"
                        android:name="com.lightbulb.pawesome.fragments.PawfileHeaderFragment"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="10dp"
                        android:fitsSystemWindows="true"
                        app:layout_collapseMode="parallax" />

                </android.support.design.widget.CollapsingToolbarLayout>

            </android.support.design.widget.AppBarLayout>

            <fragment
                android:id="@+id/pawfile_timeline"
                android:name="com.lightbulb.pawesome.user_timeline.PawesomeUserTimelineFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

UDPATE问题:

我已经完成了禁用和启用视图折叠的功能.我使用这行代码来禁用折叠:

AppBarLayout.LayoutParams appbarParams = (AppBarLayout.LayoutParams)     collapsingToolbar.getLayoutParams();
appbarParams.setScrollFlags(0);
collapsingToolbar.setLayoutParams(p);
Run Code Online (Sandbox Code Playgroud)

问题是appbar强制将scrollflags的高程设置为0.我尝试使用appbar.setElevation(0),但它没有任何效果.如何从我的appbar中删除高程?

android scroll toolbar android-collapsingtoolbarlayout

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

如何使RecyclerView的网格项具有相同的高度和宽度?

RecyclerView通过设置其布局来定义我的网格,其GridLayoutManger跨度计数为3已经正常工作.我还在我的Recycler视图中添加了一个项目装饰器,它将管理网格项之间的间距,这也可以正常工作.

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;
    private boolean includeEdge;

    public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
        this.spanCount = spanCount;
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // item position
        int column = position % spanCount; // item column

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount; // spacing …
Run Code Online (Sandbox Code Playgroud)

grid android android-recyclerview

10
推荐指数
3
解决办法
7936
查看次数

android中?attr/colorControlHighlight的默认颜色是什么?

我需要知道android中的?attr/colorControlHighlight的默认颜色,因为我需要为我的按钮应用相同的颜色按下我的drawable中的状态背景以用于棒棒糖前设备.?attr/colorControlHighlight是棒棒糖的一个属性,你不能在前棒棒糖上使用它,否则会触发错误.

android colors

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

如何在android中一次完成多个活动?

我的活动流程如此活动A - >活动B - >活动C - >活动D.当用户在活动D上并单击一个名为exit的按钮时,应用程序应返回活动B并完成活动C和D.我怎么能这样做?

注意:ACTIVITY B和ACTIVITY D属于同一类但实例不同

android android-intent android-activity

6
推荐指数
1
解决办法
2863
查看次数

如何以编程方式删除我的NestedScrollView的布局行为?

我已经实现了消除layout_scrollFlags在我的CollapsingToolbarLayout.但我需要删除layout_behavior我的,NestedScrollView以便当我的嵌套滚动视图中没有内容时,工具栏的折叠也将被禁用.删除我的NestedScrollView的layout_behavior非常简单,我只是逐字地删除xml中的代码行,但是如何以编程方式删除它?

我的xml:

<android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"

            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:background="@android:color/white">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <fragment
                        android:id="@+id/pawfile_header"
                        android:name="com.lightbulb.pawesome.fragments.PawfileHeaderFragment"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="10dp"
                        android:fitsSystemWindows="true"
                        app:layout_collapseMode="parallax" />

                </android.support.design.widget.CollapsingToolbarLayout>

            </android.support.design.widget.AppBarLayout>

            <fragment
                android:id="@+id/pawfile_timeline"
                android:name="com.lightbulb.pawesome.user_timeline.PawesomeUserTimelineFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

layout android scroll behavior android-collapsingtoolbarlayout

6
推荐指数
1
解决办法
5940
查看次数

如何使用 ACTION_VIDEO_CAPTURE 操作对相机的意图将文件的输出设置为 mp4?

当我使用本机应用程序相机拍摄视频时,输出文件的扩展名为 3gp。我需要使用 ACTION_VIDEO_CAPTURE 意图操作来意图相机,这将生成一个具有 mp4 文件扩展名的文件。我该怎么做?

mp4 camera android file android-intent

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