【问题标题】:How to pass json array using get method in retrofit android如何在改造android中使用get方法传递json数组
【发布时间】:2017-02-23 04:54:14
【问题描述】:

我是改造新手,不太了解如何使用改造传递此 json 并在我的应用程序中显示。下面是我的 json、MainActivity 和模型类。但知道它抛出异常“除了 begin_object 但找到 begin_array “。请帮帮我。

[
    {
        "userId": 1,
        "id": 1,
        "title": "sunt",
        "body": "sdfdsf"
      },
      {
        "userId": 1,
        "id": 2,
        "title": "qui est esse",
        "body": "jhmjk"
      }]

MainActivity.java

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiInterface apiService = retrofit.create(ApiInterface.class);
        // ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        Call<ModelClass> call = apiService.getLogin();
        // Call<LoginResponse> call = apiService.loginWithCredentials(new LoginRequest(email_enter, md5pass));
        call.enqueue(new Callback<ModelClass>() {
            @Override
            public void onResponse(Call<ModelClass> call, Response<ModelClass> response) {
                Log.i("REGISTRATION --->", "Registered" + response);
            }

            @Override
            public void onFailure(Call<ModelClass> call, Throwable t) {
                Log.i("REGISTRATION --->", "Throwable" + t.toString());
                // Intent i = new Intent(MainActivity.this,SecondActivity.class);
                // startActivity(i);
            }
        });
    }

ModelClass.java

public class ModelClass {
    class Model{
        public List<ObjectModel> getObjectmodel() {
            return objectmodel;
        }

        public void setObjectmodel(List<ObjectModel> objectmodel) {
            this.objectmodel = objectmodel;
        }

        List<ObjectModel> objectmodel;
    }

    class ObjectModel{
        String userId;
        String id;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getBody() {
            return body;
        }

        public void setBody(String body) {
            this.body = body;
        }

        String title;
        String body;
    }
}

【问题讨论】:

标签: android retrofit


【解决方案1】:

创建自定义模型并在改造 api 中传递相同的模型。

class Model{
   List<ObjectModel> objectmodel;
 }

class ObjectModel{
   String userId;
   String id;
   String title;
   String body;
}

在改造界面中绑定值并传递模型对象。

或者

@GET("json")
Call<JsonObject> youmethode(@Query("key") String value);

希望这会有所帮助。谢谢

【讨论】:

  • @Get() 之后如何通过这个
  • 你知道如何在改造中传递参数吗?
  • @GET("json") 调用 getDistance(@Query("origins") String origins, @Query("destinations") String destinations, @Query("key") String key );否则你可以直接在改造接口中传递json对象
  • 我只是有提供数据的 url。我不需要提供任何东西。因此如何传递参数
  • 如果您不需要提供任何东西,那么为什么要传递参数
【解决方案2】:

要传递json请求,你必须使用post方法。

【讨论】:

猜你喜欢
  • 2023-01-27
  • 2021-08-12
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
相关资源
最近更新 更多