简体   繁体   English

如何使用改型将JSON作为字符串获取

[英]How get JSON as string using retrofit

Please i'm new to retrofit and I've been stuck for weeks now, i tried getting my response as POJO class and end up with this error "Json document was not fully consumed", I've searched and also ask this question here and no one was able to help get out of this 请我是新手,现在已经被困了好几周了,我尝试以POJO类作为响应,并最终遇到此错误“ Json文档未完全使用”,我在这里搜索并提出了这个问题没有人能够帮助摆脱这个

Now i want to get my JSON as string from retrofit so i can do the conversion manually myself to my POJO class and dont know how to get the response JSON as string 现在我想从改造中获取字符串形式的JSON,这样我就可以自己手动将其转换为我的POJO类,并且不知道如何以字符串形式获取响应JSON

Please i need your help I've been stuck with this for 3 weeks 请我需要您的帮助,我已经坚持了三个星期

just set the response type as String 只需将响应类型设置为String

@GET("api/offers")
Call<String> loadOffers(); 

instead of 代替

@GET("api/offers")
Call<List<Offer>> loadOffers(); 

add this in your build.gradle file 将此添加到您的build.gradle文件中

implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'

and in public static Retrofit getRetrofitInstance() { 并在public static Retrofit getRetrofitInstance() {

add these lines at the end of the function. 在函数的末尾添加这些行。 the important part is the convert factories 重要的部分是转换工厂

if (retrofit == null) {
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

then you can fetch your data as JSON string 那么您可以将数据作为JSON字符串提取

MyApi service = RetrofitClientInstance.getRetrofitInstance().create(MyApi.class);


        Call<String> callTypes = service.loadOffers();

        callTypes.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response {                    
                String urJson  = response.body() ; 
               //  DO UR STUFF HERE
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

I hope this will help you. 我希望这能帮到您。

I hope this will help you in getting the response as string. 我希望这将有助于您获得字符串响应。

public interface RetrofitService{
        @GET("/users")
        Call<ResponseBody> listRepos();//function to call api
    }

    RetrofitService service = retrofit.create(RetrofitService.class);
    Call<ResponseBody> result = service.listRepos(username);
    result.enqueue(new Callback<ResponseBody>() {

    @Override
    public void onResponse(Response<ResponseBody> response) {
        try {
            System.out.println(response.body().string());//convert reponse to string
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(Throwable t) {
        e.printStackTrace();
    }
});

Hope this will work for you too. 希望这也对您有用。

Get the gson lib, it will do it for you. 获取gson lib,它将为您完成。 You Just have to creat an object which fit the json, and the lib will set the data in your object with you Json. 您只需要创建一个适合json的对象,lib就会与您的Json一起在您的对象中设置数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM