小编Def*_*era的帖子

定制转换器与Moshi子类

我有一个User类.还有两个子类.父母和孩子.我使用{"user":"..."}从我的服务器获取json,需要根据user.type将其转换为父级或子级.

据我所知,我需要以这种方式添加自定义转换器:

        Moshi moshi = new Moshi.Builder()
            .add(new UserAdapter())
            .build();
Run Code Online (Sandbox Code Playgroud)

这是我对UserAdapter的实现.我知道它是假的,但它甚至不能这样工作:

public class UserAdapter {

@FromJson
User fromJson(String userJson) {
    Moshi moshi = new Moshi.Builder().build();
    try {
        JSONObject jsonObject = new JSONObject(userJson);
        String accountType = jsonObject.getString("type");

        switch (accountType) {
            case "Child":
                JsonAdapter<Child> childJsonAdapter = moshi.adapter(Child.class);
                return childJsonAdapter.fromJson(userJson);
            case "Parent":
                JsonAdapter<Parent> parentJsonAdapter = moshi.adapter(Parent.class);
                return parentJsonAdapter.fromJson(userJson);

        }
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }

    return null;
}

@ToJson
String toJson(User user) {
    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<User> jsonAdapter …
Run Code Online (Sandbox Code Playgroud)

android square moshi

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

如何直接从.zip文件中读取文件而不在android中提取它

我过去几个月一直在研究android,现在我的问题是读取放在SD卡上的.zip文件.我已经成功完成了在SD卡上下载.zip文件的编码.

我有img.zip文件下载到SD卡上.这个img.zip包含5个图像文件.现在我可以直接阅读其内容而不是解压缩img.zip ...... ??? 如果是的话请帮忙.我在互联网上看到了一些例子,但他们都说解压缩然后使用,我想避免那部分,因为我只是想为图像视图设置图像.

 ImageView imv = new ImageView(this);
 imv.setImageURI(Uri.parse("//sdcard/1.png"));
Run Code Online (Sandbox Code Playgroud)

这就像下载单个图像并设置实际工作的imv源.现在我想要的是如下所示的东西.

 imv.setImageURI(Uri.parse("//sdcard/img.zip/1.png"));
Run Code Online (Sandbox Code Playgroud)

我试过这个,但在我的布局中我没有看到图像.

它可以做到...... PLZ帮助......

我通过以下代码使其工作....

 try { 
                 Bitmap mBackground=null;
                    FileInputStream fis = new FileInputStream("//sdcard/tp.zip"); 
                    ZipInputStream zis = new ZipInputStream(fis); 
                    ZipEntry ze = null; 
                    while ((ze = zis.getNextEntry()) != null) { 
                        if (ze.getName().equals("1.png")) {
                            Toast.makeText(con, "Found", 2).show();
                            mBackground = BitmapFactory.decodeStream(zis);
                            imv.setImageBitmap(mBackground);
                            break; 
                        } 
                    } 
                } catch (FileNotFoundException e) { 
                    e.printStackTrace(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 
Run Code Online (Sandbox Code Playgroud)

zip android sd-card

7
推荐指数
1
解决办法
6020
查看次数

标签 统计

android ×2

moshi ×1

sd-card ×1

square ×1

zip ×1