如何在Retrofit2中获得多个JsonArray的响应?

时间:2019-02-19 08:57:07

标签: android json retrofit2

我下面有一些JSON

 {
"Table": [
    {
        "CMBL009001": "010001",
        "NMBL009002": 0,
        "CMBL009003": "",
        "CMBL009004": "",
        "CMBL009005": "",
        "CMBL009006": "",
        "NMBL009007": 0,
        "BMBL009008": 0,
        "NMBL009009": 0,
        "CMBL009010": "ADMIN",
        "CMBL009011": "",
        "NMBL009012": 2,
        "NMBL009013": 1
    }
]
  }

这是我的模特

package com.example.showmyjsonapplicationuser;

import com.google.gson.annotations.SerializedName;

public class User {

@SerializedName("BMBL009008")
private Long mBMBL009008;
@SerializedName("CMBL009001")
private String mCMBL009001;
@SerializedName("CMBL009003")
private String mCMBL009003;
@SerializedName("CMBL009004")
private String mCMBL009004;
@SerializedName("CMBL009005")
private String mCMBL009005;
@SerializedName("CMBL009006")
private String mCMBL009006;
@SerializedName("CMBL009010")
private String mCMBL009010;
@SerializedName("CMBL009011")
private String mCMBL009011;
@SerializedName("NMBL009002")
private Long mNMBL009002;
@SerializedName("NMBL009007")
private Long mNMBL009007;
@SerializedName("NMBL009009")
private Long mNMBL009009;
@SerializedName("NMBL009012")
private Long mNMBL009012;
@SerializedName("NMBL009013")
private Long mNMBL009013;

public Long getBMBL009008() {
    return mBMBL009008;
}

public void setBMBL009008(Long bMBL009008) {
    mBMBL009008 = bMBL009008;
}

public String getCMBL009001() {
    return mCMBL009001;
}

public void setCMBL009001(String cMBL009001) {
    mCMBL009001 = cMBL009001;
}

public String getCMBL009003() {
    return mCMBL009003;
}

public void setCMBL009003(String cMBL009003) {
    mCMBL009003 = cMBL009003;
}

public String getCMBL009004() {
    return mCMBL009004;
}

public void setCMBL009004(String cMBL009004) {
    mCMBL009004 = cMBL009004;
}

public String getCMBL009005() {
    return mCMBL009005;
}

public void setCMBL009005(String cMBL009005) {
    mCMBL009005 = cMBL009005;
}

public String getCMBL009006() {
    return mCMBL009006;
}

public void setCMBL009006(String cMBL009006) {
    mCMBL009006 = cMBL009006;
}

public String getCMBL009010() {
    return mCMBL009010;
}

public void setCMBL009010(String cMBL009010) {
    mCMBL009010 = cMBL009010;
}

public String getCMBL009011() {
    return mCMBL009011;
}

public void setCMBL009011(String cMBL009011) {
    mCMBL009011 = cMBL009011;
}

public Long getNMBL009002() {
    return mNMBL009002;
}

public void setNMBL009002(Long nMBL009002) {
    mNMBL009002 = nMBL009002;
}

public Long getNMBL009007() {
    return mNMBL009007;
}

public void setNMBL009007(Long nMBL009007) {
    mNMBL009007 = nMBL009007;
}

public Long getNMBL009009() {
    return mNMBL009009;
}

public void setNMBL009009(Long nMBL009009) {
    mNMBL009009 = nMBL009009;
}

public Long getNMBL009012() {
    return mNMBL009012;
}

public void setNMBL009012(Long nMBL009012) {
    mNMBL009012 = nMBL009012;
}

public Long getNMBL009013() {
    return mNMBL009013;
}

public void setNMBL009013(Long nMBL009013) {
    mNMBL009013 = nMBL009013;
}

  }

这是我的改造班:

package com.example.showmyjsonapplicationuser;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitSingleton {
    private static Retrofit retrofit;

    public static Retrofit getInstance() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl("http://192.168.200.10:6139/api/")
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())

                    .build();
        }

        return retrofit;
    }

    private RetrofitSingleton() {

    }
    }

这是提供程序类:

package com.example.showmyjsonapplicationuser.providers;

import com.example.showmyjsonapplicationuser.ApiService;
import com.example.showmyjsonapplicationuser.RetrofitSingleton;

public class ApiServiceProvider {
    private static ApiService apiService;

    public static ApiService provideApiService() {
        if (apiService == null) {
            apiService = RetrofitSingleton.getInstance().create(ApiService.class);
        }
        return apiService;
    }
    }

这是jsonUserResponse

  package com.example.showmyjsonapplicationuser;


public class JSONUserResponse {
    private User[] Table;

    public User[] getTable(){

        return Table;
    }
        }

这是apiService

 package com.example.showmyjsonapplicationuser;

import retrofit2.Call;
import retrofit2.http.GET;

public interface ApiService {

    @GET("values?url=<NewDataSet><Table><ver>1_02.01.06</ver><proc>003TOTALSELECT</proc><P1>ADMIN</P1><P2>123456</P2><P3>MBLTYPEVISIT1</P3></Table></NewDataSet>")
    Call<JSONUserResponse> getUsersJSON();
      }

这是mainViewModel

`package com.example.showmyjsonapplicationuser;

import com.example.showmyjsonapplicationuser.providers.ApiServiceProvider;

import retrofit2.Call;

public class MainViewModel {

    private ApiService apiService = ApiServiceProvider.provideApiService();

    Call<JSONUserResponse> callUser = apiService.getUsersJSON();
    }

这是主要活动:

package com.example.showmyjsonapplicationuser;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
    public class MainActivity extends AppCompatActivity {
        private MainViewModel viewModel = new MainViewModel();
        private ArrayList<User> data = new ArrayList<>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            viewModel.callUser.enqueue(new Callback<JSONUserResponse>() {
                @Override
                public void onResponse(Call<JSONUserResponse> call, Response<JSONUserResponse> response) {

                    JSONUserResponse jsonUserResponse = response.body();
                    assert jsonUserResponse != null;
                    data = new ArrayList<>(Arrays.asList(jsonUserResponse.getTable()));
                    Toast.makeText(MainActivity.this, "اطلاعات با موفقیت دریافت شد", Toast.LENGTH_LONG).show();

            }

            @Override
            public void onFailure(Call<JSONUserResponse> call, Throwable t) {
                Log.d("Error", t.getMessage());


            }
        });



    }
      }

问题在于响应为空。

1 个答案:

答案 0 :(得分:0)

您需要创建两个模型类,一个如下所示

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

公共类表{

@SerializedName("CMBL009001")
@Expose
private String cMBL009001;
@SerializedName("NMBL009002")
@Expose
private Integer nMBL009002;
@SerializedName("CMBL009003")
@Expose
private String cMBL009003;
@SerializedName("CMBL009004")
@Expose
private String cMBL009004;
@SerializedName("CMBL009005")
@Expose
private String cMBL009005;
@SerializedName("CMBL009006")
@Expose
private String cMBL009006;
@SerializedName("NMBL009007")
 //getters and setters
}

另一个将是

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class TestModel {

@SerializedName("Table")
@Expose
private List<Table> table = null;

public List<Table> getTable() {
return table;
}

public void setTable(List<Table> table) {
this.table = table;
}
}

然后您可以在调用中使用TestModel。