在Retrofit Parsing中没有来自url的回复

时间:2018-03-14 18:38:57

标签: android parsing retrofit

我正在使用改进实现json解析,但我没有从url获得任何响应。 Json数据采用Array的形式,其中有对象。我试图在TextView中设置来自Url的响应。我已经实现如下:

我的JSON结构如下:

[{
    "id": "2",
    "name": "Nbn",
    "code": "001",
    "ecozone": "ter",
    "devregion": "east"
},
{
    "id": "3",
    "name": "hari",
    "code": "002",
    "ecozone": "hill",
    "devregion": "west"
}
]

Api服务

public interface ApiService {
@GET("/data_new.php?q=district")
Call<District> getMyJSON();
}

RetrofitClient

public class RetrofitClient {
private static final String url = "url/";

private static Retrofit getRetrofitInstance() {
    OkHttpClient client=new OkHttpClient();
    return new Retrofit.Builder()
            .baseUrl(url)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}
public static ApiService getApiService(){
    return getRetrofitInstance().create(ApiService.class);
}
}

我的ModelDto类

public class DistrictDTO {
@SerializedName("id")
@Expose
String id;

public String getId() {
    return id;
}

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

@SerializedName("name")
@Expose
String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@SerializedName("code")
@Expose
String code;

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

@SerializedName("ecozone")
@Expose
String ecozone;

public String getEcozone() {
    return ecozone;
}

public void setEcozone(String ecozone) {
    this.ecozone = ecozone;
}

@SerializedName("devregion")
@Expose
String devregion;

public String getDevregion() {
    return devregion;
}

public void setDevregion(String devregion) {
    this.devregion = devregion;
}


}

DistrictDTO

public class District {
public ArrayList<DistrictDTO> districtDTOS;

public ArrayList<DistrictDTO> getDistrictDTOS() {
    return districtDTOS;
}

public void setDistrictDTOS(ArrayList<DistrictDTO> districtDTOS) {
    this.districtDTOS = districtDTOS;
}
}

活动

public class DistrictActivity extends AppCompatActivity {
private ArrayList<DistrictDTO> district;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_district);
    district = new ArrayList<>();
    textView = (TextView) findViewById(R.id.textVi);
    ApiService api = RetrofitClient.getApiService();
    retrofit2.Call<District> call = api.getMyJSON();
    call.enqueue(new Callback<District>() {
        @Override
        public void onResponse(retrofit2.Call<District> call, Response<District> response) {

            if (response.isSuccessful()) {
                //got successfully
                district = response.body().getDistrictDTOS();
                Log.e("Respose", "" + district);
                textView.setText(response.body().getDistrictDTOS().toString());
            }
        }
        @Override
        public void onFailure(retrofit2.Call<District> call, Throwable t) {

            t.fillInStackTrace();
        }
    });
}

}

2 个答案:

答案 0 :(得分:1)

public interface ApiService {
@GET("/data_new.php?q=district")
Call<District[]> getMyJSON();
}

您正在调用数组,而不是对象

编辑:

实际上,我认为您应该将类​​DistrictDTO重命名为District,并删除另一个。并且,将District[]更改为您想要该数组的所有部分

答案 1 :(得分:0)

只需进行这些更改即可(返回数组而不是对象

 public class DistrictActivity extends AppCompatActivity {
private ArrayList<DistrictDTO> district;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_district);
    district = new ArrayList<>();
    textView = (TextView) findViewById(R.id.textVi);
    ApiService api = RetrofitClient.getApiService();
    retrofit2.Call<List<DistrictDTO>> call = api.getMyJSON();
    call.enqueue(new Callback<List<DistrictDTO>>() {
        @Override
        public void onResponse(retrofit2.Call<List<DistrictDTO>> call, Response<List<DistrictDTO>> response) {

            if (response.isSuccessful()) {
                //got successfully
                district = response.body().toString();
                Log.e("Respose", "" + district);
                textView.setText(response.body().toString());
            }
        }
        @Override
        public void onFailure(retrofit2.Call<List<DistrictDTO>> call, Throwable t) {
Toast.makeText(DistrictActivity.this,t.toString(),Toast.LENGTH_SHORT).show();
            t.fillInStackTrace();
        }
    });
}

}

,你的界面应该是这样的

    public interface ApiService {
@GET("/data_new.php?q=district")
Call<List<DistrictDTO>> getMyJSON();
}

事实上你应该期待一个数组而不是像上面那样的对象,只返回DistrictDTO的列表,不需要District

仅供参考:当来自服务器的响应的格式为{..}时,您应该期望一个对象(就像您所做的那样),但是当它像[..]那样是一个json数组时,你应该期待一个数组(这是你的情况,就像我在答案中所做的那样)