2016-10-05 7 views
-3

Retrofitjsonの形式を読むにはどうすればよいですか?json with Retrofit - アンドロイドを読む?

{"Key":null,"Response":1} 

NOTICE

私が使用しています:

compile 'com.squareup.okhttp3:okhttp:3.4.1' 
compile 'com.google.code.gson:gson:2.6.1' 
compile 'com.squareup.retrofit2:retrofit:2.1.0' 
compile 'com.squareup.retrofit2:converter-gson:2.1.0' 

私のコードは次のとおりです。

private void loadJSON(String username, String password) { 
    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("http://192.168.8.11:8087/") 
      .addConverterFactory(GsonConverterFactory.create(new Gson())) 
      .build(); 

    RequestInterface_Login request = retrofit.create(RequestInterface_Login.class); 
    Call<JSONResponseLogin> call = request.getJSON(username, password); 
    call.enqueue(new Callback<JSONResponseLogin>() { 
     @Override 
     public void onResponse(Call<JSONResponseLogin> call, Response<JSONResponseLogin> response) { 

      JSONResponseLogin jsonResponse = response.body(); 
      data = new ArrayList<>(Arrays.asList(jsonResponse.getLogin())); 
     } 

     @Override 
     public void onFailure(Call<JSONResponseLogin> call, Throwable t) { 
      Log.d("Error", t.getMessage()); 
     } 
    }); 
} 

そしてJSONResponseLogin

public class JSONResponseLogin { 
    private ModelLogin[] login; 

    public ModelLogin[] getLogin() { 
     return login; 
    } 
} 

そしてModelLogin

public class ModelLogin { 
    private Object Key; 
    private Integer Response; 

    public Object getKey() { 
     return Key; 
    } 

    public Integer getResponse() { 
     return Response; 
    } 
} 

そしてRequestInterface_Login

public interface RequestInterface_Login { 

    @GET("/courier.svc/login") 
    Call<JSONResponseLogin> getJSON(@Query("username") String username, @Query("password") String password); 
} 

クラッシュ私:

java.lang.NullPointerException: storage == null 

enter image description here

+0

投稿したものがあなたのコードのどの行も指していないので、完全なstacktraceを入れてください。 – jakubbialkowski

答えて

0

{ "キー":ヌル、 "応答":1}

使用上記のJSONのこのPOJOクラス....

public class Result { 

@SerializedName("Key") 
@Expose 
private Object key; 
@SerializedName("Response") 
@Expose 
private Integer response; 

/** 
* 
* @return 
* The key 
*/ 
public Object getKey() { 
return key; 
} 

/** 
* 
* @param key 
* The Key 
*/ 
public void setKey(Object key) { 
this.key = key; 
} 

/** 
* 
* @return 
* The response 
*/ 
public Integer getResponse() { 
return response; 
} 

/** 
* 
* @param response 
* The Response 
*/ 
public void setResponse(Integer response) { 
this.response = response; 
} 

} 

と、このメソッドを呼び出す....

Call<Result> call = request.getJSON(username, password); 
    call.enqueue(new Callback<Result>() { 
     @Override 
     public void onResponse(Call<Result> call, Response<Result> response) { 
      if (response.isSuccessful()) { 
       //statements on sucesss  
       }else{ 
       //statements when server have problem or Api problem 
        } 
     } 

     @Override 
     public void onFailure(Call<Result> call, Throwable t) { 
      Log.d("Error", t.getMessage()); 
     } 
    }); 
} 
0

値がnullになることが予想される場合は、Stirngまたはintではなくオブジェクト型を使用する必要があります。適切なレスポンスを得るには、Pojoクラスの下で使用してください。

public class Response { 

    private Object Key; 
    private int Response; 

    public Object getKey() { 
     return Key; 
    } 

    public void setKey(Object Key) { 
     this.Key = Key; 
    } 

    public int getResponse() { 
     return Response; 
    } 

    public void setResponse(int Response) { 
     this.Response = Response; 
    } 
}