android – 从改造响应中获取JSON数组

前端之家收集整理的这篇文章主要介绍了android – 从改造响应中获取JSON数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要从Retrofit解析 JSON数组.我需要获得以下密钥:
{
  "rc":0,"message":"success","he":[
    {
      "name":"\u05de\u05e4\u05e7\u05d7","type":0
    }
  ]
}

我可以很容易地得到消息,但我无法从响应中得到“他”数组.

这是我的数据模型类

public class GetRoleData implements Serializable {

    @SerializedName("he")

    private ArrayList<Roles> he;

    @SerializedName("message")
    private String message;

    public GetRoleData() {
        this.he = new ArrayList<>();
        this.message = "";
    }

    public ArrayList<Roles> getUserRoles() {
        return he;
    }

    public String getMessage() {
        return message;
    }

    public class Roles {

        public Roles() {
            name = "";
            type = -1;
        }

        @SerializedName("name")

        private String name;
        @SerializedName("type")

        private int type;

        public int getType() {
            return type;
        }

        public String getName() {
            return name;
        }

    }
}

这就是我向服务器发送请求的方式:

@POST("index.PHP/")
Call<GetRoleData> getUserRoles(@Body SetParams body);

这是我如何发送请求和处理响应

APIService apiService = retrofit.create(APIService.class);

        Call<GetRoleData > apiCall = apiService.getUserRoles(params);
        apiCall.enqueue(new Callback<GetRoleData >() {


            @Override
            public void onResponse(retrofit.Response<GetRoleData > mUserProfileData,Retrofit retrofit) {

                Log.e("locale info","mUserProfileData = " + mUserProfileData.body().toString());
                if (pDialog != null) {
                    pDialog.dismiss();
                }
                if (mUserProfileData.body().getMessage().equals("success")) {

                    Log.e("locale info","user roles = " + mUserProfileData.body().getUserRoles().size());

                } else {
                    Toast.makeText(RegisterActivity.this,getResources().getString(R.string.get_role_error),Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Throwable t) {

                if (pDialog != null) {
                    pDialog.dismiss();
                }

                t.printStackTrace();
            }
        });

我想要的是

我需要从上面的响应中获取“he”数组.请帮忙谢谢.

这是我得到的回应..

解决方法

更新改造2.0-beta2:
dependencies {
    compile fileTree(dir: 'libs',include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.google.code.gson:gson:2.4'
    compile 'com.squareup.okhttp:okhttp:2.5.0'
    // compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
}

接口:

@GET("/api/values")
Call<GetRoleData> getUserRoles();

MainActivity的onCreate:

// Retrofit 2.0-beta2
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API_URL_BASE)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        WebAPIService service = retrofit.create(WebAPIService.class);

        // Asynchronous Call in Retrofit 2.0-beta2
        Call<GetRoleData> call = service.getUserRoles();
        call.enqueue(new Callback<GetRoleData>() {
            @Override
            public void onResponse(Response<GetRoleData> response,Retrofit retrofit) {
                ArrayList<GetRoleData.Roles> arrayList = response.body().getUserRoles();
                if (arrayList != null) {
                    Log.i(LOG_TAG,arrayList.get(0).getName());
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e(LOG_TAG,t.toString());
            }
        });

改造1.9

我使用你的GetRoleData类

界面:

public interface WebAPIService {        

    @GET("/api/values")
    void getUserRoles(Callback<GetRoleData> callback);                       
}

主要活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);            

        // creating a RestAdapter using the custom client
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(API_URL_BASE)
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setClient(new OkClient(mOkHttpClient))
                .build();

        WebAPIService webAPIService = restAdapter.create(WebAPIService.class);

        Callback<GetRoleData> callback = new Callback<GetRoleData>() {
            @Override
            public void success(GetRoleData getRoleData,Response response) {
                String bodyString = new String(((TypedByteArray) response.getBody()).getBytes());
                Log.i(LOG_TAG,bodyString);
            }

            @Override
            public void failure(RetrofitError error) {
                String errorString = error.toString();
                Log.e(LOG_TAG,errorString);
            }
        };

        webAPIService.getUserRoles(callback);
    }

屏幕截图如下:

原文链接:https://www.f2er.com/android/316042.html

猜你在找的Android相关文章