2017-11-27 5 views
0

data.police.uk Webサイトから配列を取得しようとしています。URL nullオブジェクト参照からJsonを取得しています

残念ながら、コードを実行しているときにエラーがアップします:

package com.example.cosmin.crimerate.Latest_crimes; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.example.cosmin.crimerate.R; 

import java.util.List; 

import retrofit2.Call; 
import retrofit2.Callback; 
import retrofit2.Response; 
import retrofit2.Retrofit; 
import retrofit2.converter.gson.GsonConverterFactory; 

public class JsonCrimesActivity extends AppCompatActivity { 

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

     final ListView listView = findViewById(R.id.crimesList); 


     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(Api.BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

     Api api = retrofit.create(Api.class); 

     Call<List<Crime>> call = api.getCrimes(); 

     call.enqueue(new Callback<List<Crime>>() { 
      @Override 
      public void onResponse(Call<List<Crime>> call, Response<List<Crime>> response) { 
       List<Crime> Crimes = response.body(); 

       String[] crimeCategories = new String[Crimes.size()]; 

        for (int i=0; i < Crimes.size();i++){ 
         crimeCategories[i]= Crimes.get(i).getCategory(); 
        } 

        listView.setAdapter(
          new ArrayAdapter<String>(
            getApplicationContext(), 
            android.R.layout.simple_list_item_1, 
            crimeCategories 

          ) 
        ); 

       } 


      @Override 
      public void onFailure(Call<List<Crime>> call, Throwable t) { 
       Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show(); 

      } 
     }); 


    } 
} 

APIクラスです:

public interface Api{ 

    String BASE_URL = "https://data.police.uk/api/"; 

    @GET("/crimes-no-location?category=all-crime&force=leicestershire&date=2017-02") 
    Call<List<Crime>> getCrimes(); 
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

ここでは、コード、私が間違っているの何ノーアイデアは、です

これは配列のためだと思いますか?

私にヒントや何が起こっているのかを教えてください。

ありがとうございました!

LE:犯罪MODEL:正常に動作している場合

public class Crime { 

    private String category; 
    private String persistent_id; 
    private String location_subtype; 
    private String id; 
    private String location; 
    private String context; 
    private String month; 
    private String location_type; 
    private String outcome_status; 

    public Crime(String category, String persistent_id, String location_subtype, String id, String location, String context, String month, String location_type, String outcome_status) { 
     this.category = category; 
     this.persistent_id = persistent_id; 
     this.location_subtype = location_subtype; 
     this.id = id; 
     this.location = location; 
     this.context = context; 
     this.month = month; 
     this.location_type = location_type; 
     this.outcome_status = outcome_status; 

    } 

    public String getCategory() { 
     return category; 
    } 

    public String getPersistent_id() { 
     return persistent_id; 
    } 

    public String getLocation_subtype() { 
     return location_subtype; 
    } 

    public String getId() { 
     return id; 
    } 

    public String getLocation() { 
     return location; 
    } 

    public String getContext() { 
     return context; 
    } 

    public String getMonth() { 
     return month; 
    } 

    public String getLocation_type() { 
     return location_type; 
    } 

    public String getOutcome_status() { 
     return outcome_status; 
    } 
} 
+0

'response.body()'何を返さずに先頭のスラッシュを削除しますようになり、次のURIへのHTTP呼び出し(気づくダブル//)を作っていますか? – joao86

+0

まず、あなたのURLに余分な '/'があることに気がつき、それを削除してもう一度テストしてください。問題がまだ残っている場合は、あなたの犯罪モデルクラスを分かち合うことができますか? –

+0

あなたの返信ありがとう。 /を削除しました。残念ながら、新しいエラーが発生します。java.lang.IllegalStateException:文字列が必要ですが、行1の列93の$ BのBEGIN_OBJECTのパス$ [0] .outcome_statusでした。 – SamSu

答えて

0

は、コードのこの部分を確認してください。

List<Crime> Crimes = response.body(); 

あなたの犯罪のリストは、おそらく空またはnullです。

0

nullです。あなたの設定が間違っていると確信しています。Api

https://data.police.uk/api//crimes-no-location?category=all-crime&force=leicestershire&date=2017-02 

があなたの@GET

+0

あなたの返信をありがとう。 /を削除しました。残念ながら、新しいエラーが発生します。java.lang.IllegalStateException:文字列が必要ですが、行1の列93の$ BのBEGIN_OBJECTのパス$ [0] .outcome_statusでした。 – SamSu

+0

あなたには複数の問題がありました。つまり、モデルの解析に問題がある可能性があります。 'outcome_status'は文字列を期待していますが、代わりにjsonオブジェクトを返します。それを考慮に入れてモデルを更新する必要があります。あなたのエンドポイントから返されたjsonを注意深く見てください(私はここでうまくフォーマットしました:https://pastebin.com/tWV7rhvS) – Jon

関連する問題