安卓積累

請問爲什麼Retrofit以Mutipart上傳參數時,String參數會多一對雙引號

效果:就是後臺數據庫接受的參數會多出雙引號,就是比如:傳666,變成了"666"

情況1:爲了獲取json的javaBean對象,註冊了 GsonConverter,沒註冊標準類型數據的轉換器

.addConverterFactory(GsonConverterFactory.create()),

 public static Retrofit fileRetrofit(){
        if (mFileRetrofit == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .addInterceptor(new LoggingInterceptor());
            OkHttpClient okHttpClient = builder.build();

            mFileRetrofit = new Retrofit.Builder()
                    .baseUrl(ApiStores.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();
        }
        return mFileRetrofit;
    }

“@Part(“data”) String des”在Post請求中默認的Content-Type類型是“application/json”,這就說明我們在接口中不能再使用@Part註解了

@Multipart
@POST("userPhoto")
Observable<BaseHttpResult<String>> uploadMultipleTypeFile(@PartMap Map<String, RequestBody> params);

Map<String, RequestBody> bodyMap = new HashMap<>();
bodyMap.put("photo"; filename=""+file.getName(), RequestBody.create(MediaType.parse("image/png"),file));
bodyMap.put("userId", toRequestBody(userId));
bodyMap.put("serialNumber", toRequestBody(serialNumber));

public static RequestBody toRequestBody(String value) {

    RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), value);
    return requestBody;

}

 情況2:

原因是你註冊了 GsonConverter,沒註冊標準類型數據的轉換器,導致String這些都會轉成 JSON 傳輸,你只需要加一個標準類型的轉換器就行了。

這樣返回的就是json的字符串

implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
.addConverterFactory(ScalarsConverterFactory.create())

Android 8.0踩坑記錄——Only fullscreen opaque activities can request orientation

有一句是這樣的Only fullscreen opaque activities can request orientation,也就是說只有全屏不透明的activity纔可以設置方向,既然知道問題所在就好辦了。

原因

出現這樣的問題,絕大多數都是因爲我們爲了提高用戶體驗,手動取消App啓動白屏或者黑屏的時候,將Splash界面設爲了透明,然後這個時候又設置了方向爲垂直,從而導致了這個問題。

如何解決app啓動白屏(黑屏)請參考我之前的文章
https://www.jianshu.com/p/c24058c3d385

解決

重點來了,解決辦法其實很簡單

1.找到你設置透明的Activity,然後在他的theme中將android:windowIsTranslucent改爲false
<item name="android:windowIsTranslucent">false</item>

2.再加入<item name="android:windowDisablePreview">true</item>就搞定了。

鏈接:https://www.jianshu.com/p/d0d907754603

gson 處理泛型

今天我就碰到了處理泛型時的問題

使用的實體類如下:

class Option {
    public String itemValue;
    public String itemLabel;

    public Option(String itemValue, String itemLabel) {
        this.itemValue = itemValue;
        this.itemLabel = itemLabel;
    }
}

(1)將List變成json字符串

 

List<Option> options = new ArrayList<Option>();
options.add(new Option("1", "男"));
options.add(new Option("2", "女"));
Gson gson = new Gson();
String json = gson.toJson(options, List.class);
System.out.println(json);

 

打印出[{"itemValue":"1","itemLabel":"男"},{"itemValue":"2","itemLabel":"女"}]

(2)將上面的字符串轉成List

String json = 上面的輸出
Gson gson = new Gson();
List<Option> options = gson.fromJson(json,List.class);
for (Iterator it = options.iterator(); it.hasNext();) {
    Option option = (Option) it.next();
    System.out.println(option.itemLabel);
}

 

報錯如下:

Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.StringMap cannot be cast to
Option

改成

Gson gson = new Gson();
List<Option> options = gson.fromJson(json,new TypeToken<List<Option>>(){}.getType());
for (Iterator it = options.iterator(); it.hasNext();) {
    Option option = (Option) it.next();
    System.out.println(option.itemLabel);
}

成功!

 

EventBus使用無效:

情況:傳遞的是boolean類型的值:

使用簡單的方式,無效,

 @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(boolean isSelectFrom) {
        LogUtils.d("isSelectFrom="+isSelectFrom);
        ToastUtils.showToast(this, "isSelectFrom=" + isSelectFrom);
        if (isSelectFrom) {
//            finish();
        }
    }

必須使用對象:

@Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        /* Do something */
        LogUtils.d("isSelectFrom="+event.isSelectFrom);
        ToastUtils.showToast(this, "isSelectFrom=" + event.isSelectFrom);
        if (event.isSelectFrom) {
                        finish();
        }
    };

android alpha設置透明度,屏蔽底層點擊事件

android 透明佈局點擊穿透的處理

今天在做一個彈出層時,由於可以透過透明彈出層點擊到下面的控件從而導致一個BUG的產生,後來我找資料一查得知:

  解決這個問題可以採用以下兩種方法:

一種是:在彈出層的佈局中加入     android:clickable="true"

第二種:則是在java代碼中給彈出層設置

彈出層.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return true;
}
});

 

Android自定義Shape 加上陰影shadow之方法

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 陰影部分 -->
    <!-- 個人覺得更形象的表達:top代表下邊的陰影高度,left代表右邊的陰影寬度。其實也就是相對應的offset,solid中的顏色是陰影的顏色,也可以設置角度等等 -->
    <item
        android:width="151dp"
        android:height="95dp"
        android:left="2dp"
        >
        <shape android:shape="rectangle">
            <gradient
                android:angle="270"
                android:endColor="#0F000000"
                android:startColor="#0F000000"/>

            <corners
                android:bottomLeftRadius="3dp"
                android:bottomRightRadius="3dp"
                android:topLeftRadius="3dp"
                android:topRightRadius="3dp"/>
        </shape>
    </item>

    <item
        android:width="151dp"
        android:height="91dp"
        android:top="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffffff"/>
            <corners
                android:bottomLeftRadius="3dp"
                android:bottomRightRadius="3dp"
                android:topLeftRadius="3dp"
                android:topRightRadius="3dp"/>

        </shape>
    </item>
    <item
        android:width="5dp"
        android:height="91dp"
        android:top="2dp">
        <shape android:shape="rectangle">
            <solid android:color="#fff4877a"/>
            <corners
                android:bottomLeftRadius="3dp"
                android:bottomRightRadius="0dp"
                android:topLeftRadius="3dp"
                android:topRightRadius="0dp"/>
        </shape>
    </item>
</layer-list>

效果:

walle gradlew打包報錯

gradlew assembleReleaseChannels

1找不到這個命令任務

2運行提示的 gradlew tasks

3提示告訴我們,需要運行具體的渠道名指令

//product flavors
    productFlavors {


        tencent {
            dimension "color"
            manifestPlaceholders = [UMENG_CHANNEL: "tentcent"]
        }

        xiaomi {
            dimension "color"
            manifestPlaceholders = [UMENG_CHANNEL: "xiaomi"]
        }

    }

如打出小米的渠道:

gradlew  assembleXiaomiDebugChannels 

 

android.content.res.Resources$NotFoundException: String resource ID

TextView 有個setText(int reid) 方法,如果我們從網絡上獲取到的數據是int 不是String 然後就調用瞭如下的方法:這個時候不會提示錯誤,在運行的時候回找不到資源報錯。素顏要檢查下類型。 

text.setText(info.getCnt());

參考:https://blog.csdn.net/owenchan1987/article/details/72824096

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章