获取JSON数据时的非法状态异常

时间:2018-02-19 13:08:09

标签: java json rest

我在使用rest api以json格式获取数据时遇到问题。我在两个不同的json源中尝试了这种方式,并且都给出了非法状态异常。这是我的代码

MainActivity

public class MainActivity extends AppCompatActivity {

List<NetworksItem> list;
DataAdapter dataAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = findViewById(R.id.listview);
    istek();
}

public void istek(){
    list = new ArrayList<>();
    Call<List<NetworksItem>> call = ManagerAll.getInstance().getirData();
    call.enqueue(new Callback<List<NetworksItem>>() {
        @Override
        public void onResponse(Call<List<NetworksItem>> call, Response<List<NetworksItem>> response) {
            if(response.isSuccessful())
            {
                list = response.body();
                Log.i("bilgiler" , response.body().toString());
                dataAdapter = new DataAdapter(getApplicationContext(), list, MainActivity.this);
                listView.setAdapter(dataAdapter);
            }
        }
        @Override
        public void onFailure(Call<List<NetworksItem>> call, Throwable t) {
            Log.i("hata", t.getClass().toString());


        }
    });
}
}

模型数据

public class NetworksItem{
private String name;
private List<String> company;
private Location location;
private String href;
private String id;

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

public String getName(){
    return name;
}

public void setCompany(List<String> company){
    this.company = company;
}

public List<String> getCompany(){
    return company;
}

public void setLocation(Location location){
    this.location = location;
}

public Location getLocation(){
    return location;
}

public void setHref(String href){
    this.href = href;
}

public String getHref(){
    return href;
}

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

public String getId(){
    return id;
}

@Override
public String toString(){
    return 
        "NetworksItem{" + 
        "name = '" + name + '\'' + 
        ",company = '" + company + '\'' + 
        ",location = '" + location + '\'' + 
        ",href = '" + href + '\'' + 
        ",id = '" + id + '\'' + 
        "}";
    }
}

DataAdapter的

 public class DataAdapter extends BaseAdapter{

    private Context context;
    private List<NetworksItem> list;
    private Activity activity;

    public DataAdapter(Context context, List<NetworksItem> list, Activity activity) {
        this.context = context;
        this.list = list;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.listviewelement, viewGroup, false);
        TextView name = view.findViewById(R.id.name);
        name.setText(list.get(i).getName().toString());
        return view;
    }
}

休息班

public class BaseManager {

protected RestApi getRestApiClient(){
    RestApiClient restApiClient = new RestApiClient(BaseUrl.Data_url);
    return  restApiClient.getRestApi();
}
}

public class BaseUrl {

public static final String Data_url = "http://api.citybik.es/v2/";
}

public class ManagerAll extends BaseManager {

private static ManagerAll ourInstance = new ManagerAll();

public static synchronized ManagerAll getInstance(){
    return ourInstance;
}

public Call<List<NetworksItem>> getirData(){
    Call<List<NetworksItem>> call = getRestApiClient().bilgiGetir();
    return call;
}

}

public interface RestApi {

@GET("networks")
Call<List<NetworksItem>> bilgiGetir();
}

public class RestApiClient {

private RestApi restApi;

public RestApiClient (String restApiServiceUrl) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder()
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .connectTimeout(3, TimeUnit.SECONDS);

    OkHttpClient okHttpClient = builder.build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(restApiServiceUrl)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    restApi = retrofit.create(RestApi.class);

}

public RestApi getRestApi(){
    return restApi;
}
}

我无法弄清楚导致非法状态异常的原因。来源没有问题。

0 个答案:

没有答案