出现“尝试获取空数组的长度”错误

时间:2019-05-26 03:13:02

标签: android rest

我收到“尝试获取空数组的长度”错误。 当我尝试单击按钮时,尝试从链接https://gturnquist-quoters.cfapps.io/api/random中获取以下随机报价: 输入“成功” 值
7号 quote“但是,Boot的真正好处是,它只是Spring。这意味着代码朝着任何方向发展,无论复杂性如何,我都知道这是一个安全的选择。”

MainActivity.java

public class MainActivity extends AppCompatActivity {

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


        btnCallRestApi = findViewById(R.id.btnCallRestApi);
        btnCallRestApi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //call Rest Api
                new HttpReqTask().execute();

            }
        });
    }
    private class HttpReqTask extends AsyncTask<Void, Void, Value[]> {
        @Override
        protected Value[] doInBackground(Void... params){
            try{
                String apiUrl = "https://gturnquist-quoters.cfapps.io/api/random";
                RestTemplate rest = new RestTemplate();
                rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
                Value[] quotes = rest.getForObject(apiUrl, Value[].class);
                return quotes;
            }catch (Exception ex){
                Log.e("Could not get Quotes",ex.getMessage());
            }
            return null;
        }
        protected void onPostExecute(Value[] quotes){
            super.onPostExecute(quotes);
            for(Value v:quotes){
                Log.i("id: ", String.valueOf(v.getId()));
                Log.i("quote: ", v.getQuote());
            }
        }
    }
}

Quote.java

 public Quote() {
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Value getValue() {
        return value;
    }

    public void setValue(Value value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Quote{" +
                "type='" + type + '\'' +
                ", value=" + value +
                '}';
    }

Value.java

 public Value() {
    }

    public Long getId() {
        return this.id;
    }

    public String getQuote() {
        return this.quote;
    }

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

    public void setQuote(String quote) {
        this.quote = quote;
    }

    @Override
    public String toString() {
        return "Value{" +
                "id=" + id +
                ", quote='" + quote + '\'' +
                '}';
    }

1 个答案:

答案 0 :(得分:0)

您尝试在null数组上执行循环,该循环触发NPE,并尝试使用try/catch子句

 protected void onPostExecute(Value[] quotes){
            super.onPostExecute(quotes);   
    try {
        for(Value v:quotes){
            Log.i("id: ", String.valueOf(v.getId()));
            Log.i("quote: ", v.getQuote());
        }
    } catch (NullPointerException e) {
        System.out.print("Caught the NullPointerException");
    }

编辑以查找新问题:

我认为您错误地阅读了JSON对象!