Tag: android gson

Kotlin okhttp3更改json方法为“Post”

val request = Request.Builder() .url(url) .method(“POST”,null) .addHeader(“_Token”,””) .addHeader(“Content-Type”,”application/json”) .build() 这段代码抛出一个exception。 如何添加方法types和标题? 谢谢你的回复。 引起:java.lang.reflect.InvocationTargetException引起:java.lang.IllegalArgumentException:方法POST必须有一个请求体。

为什么json字符串为空时,我将一个内部对象传递给Gson()。toJson(object)in Kotlin?

我使用Kotlin中的Gson将对象转换为json字符串。 var json2可以返回正确的结果,但var json1返回null,为什么? import android.os.Bundle import android.support.v7.app.AppCompatActivity import bll.SettingManage import info.dodata.mirror.R import model.MSetting import com.google.gson.Gson import utility.PreferenceTool class UIMain : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.layout_main) data class WiFiDefA(val Name:String, val Status: String) var aWiFiDefA=WiFiDefA(“a”,”b”) var json1=Gson().toJson(aWiFiDefA) var aWiFiDefB=WiFiDefB(“c”,”d”) var json2=Gson().toJson(aWiFiDefB) } data class WiFiDefB(val Name:String, val Status: String) }

Kotlin Gson反序列化

我正在接收具有地图包装器表的JSON数据模型。 我试图使用generics来传递超越包装的types,但它不能很好地在运行时翻译。 这里是我的JSON文件的一个例子: { “Table”: [ { “paymentmethod_id”: 1, “paymentmethod_description”: “Cash”, “paymentmethod_code”: “Cash”, “paymentmethod_is_ach_onfile”: false, “paymentmethod_is_element”: false, “paymentmethod_is_reward”: false, “paymentmethod_is_openedgeswipe”: false, “paymentmethod_update_user_id”: 1, “paymentmethod_insert_user_id”: 1, “paymentmethod_insertdate”: “2014-10-07 14:53:16”, “paymentmethod_deleted”: false, “paymentmethod_is_mobile_visible”: true } ] } 我使用的包装类叫做Table。 data class Table( @SerializedName(“Table”) val models : Array ) 实际的模型类是PaymentMethod。 data class PaymentMethod( @SerializedName(“paymentmethod_id”) val idNumber : Int = […]

Kotlin使用Gson将json字符串转换为ojbect列表

我有一个问题,Kotlin编写从json字符串转换为对象列表的代码。 在Java中通常是这样的: Gson gson = new Gson(); Type type = new TypeToken<List>() {}.getType(); List measurements = gson.fromJson(json, type); return measurements; 然而在Kotlin,当我尝试像这样: val gson = Gson() val type: Type = TypeToken<List>{}.type val measurements : List = gson.fromJson(text, type) return measurements IDE Android Studio强调为TypeToken所说的错误: 无法访问”:它是public / package / in’TypeToken’ 也强调说错误: types不匹配。 必需:键入! 发现:()→单位 那么有没有一种解决方案可以让Kotlin工作?

POJO字段声明,设置为init值或null

在我的POJO中,我通常会声明这样的字段 class SampleModel { var field1: String? = null var field2: Int? = null <more fields here> } 如果我这样声明,我将需要检查该字段是否为空。 但如果这样的话: class SampleModel { var field1 = "" var field2 = 0 <more fields here> } 我可以直接使用它。 使用哪个声明有什么关系? 就记忆力,表现力而言,还是一个很好的做法? 可以说我有100个领域。 然后我使用Moshi或Gson作为解串器。

ResponseBody给String非常慢

将我的OkHttp3 ResponseBody对象转换为字符串需要8秒(在模拟器上,在真实设备上,速度慢但速度慢)。 这太慢了。 我能加快速度吗? val time = System.currentTimeMillis() android.util.Log.i("start=", "") val body = result.body()?.string() android.util.Log.i("end=", "" + (System.currentTimeMillis()-time)) 这是有效载荷: http : //jimclermonts.nl/interpolis/json

Gson或Moshi:在POJO领域可以有两种类型,如何保存到任何领域

编辑: 这是我有的JSON字符串: json#1 { [ { field1 : "" field2 : 0 field3 : "Amount not fixed" or field : 250 // this field can be string or int }, { field1 : "" field2 : 0 field3 : "Amount not fixed" or field : 250 // this field can be string or int } […]

在Kotlin中使用GsonTypeAdapter的AutoValue

我正在尝试将Kotlin集成到现有的Android Java项目中。 在构建和添加Kotlin到项目中的更改后,我找不到解决方案,我的AutoValue类具有GsonTypeAdapter似乎不支持这一点。 @AutoValue public abstract class MediaObject implements Parcelable { public static TypeAdapter<MediaObject> typeAdapter(Gson gson) { return new AutoValue_MediaObject.GsonTypeAdapter(gson); } @SerializedName("mimetype") public abstract String getMimeType(); @SerializedName("url") public abstract String getUri(); } My Gson builder: GsonBuilder().registerTypeAdapterFactory(new AutoValueGsonTypeAdapterFactory()) 任何建议如何解决这个问题或使用什么?

在Kotlin中使用TypeAdapter实现TypeAdapterFactory

我正在尝试在我的android项目中使用Kotlin语言实现一些特定的GSON TypeAdapter。 我正面临的问题是编译错误,无法推断类型: Type inference failed: 'T' cannot capture 'in (T..T?'. Type parameter has an upper bound 'Enum<T>' that cannot be satisfied capturing 'in' projection 代码如下: class SmartEnumTypeAdapterFactory(fallbackKey: String) : TypeAdapterFactory { private val fallbackKey = fallbackKey.toLowerCase(Locale.US) override fun <T : Any> create(gson: Gson?, type: TypeToken<T>): TypeAdapter<T>? { val rawType = type.rawType return if (!rawType.isEnum) […]