2016-10-05 5 views
-3

Wie kann ich lesen bellow format json mit Retrofit?JSON mit Retrofit lesen - Android?

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

HINWEIS

ich verwende:

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' 

Meine Codes ist:

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()); 
     } 
    }); 
} 

Und JSONResponseLogin:

public class JSONResponseLogin { 
    private ModelLogin[] login; 

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

Und ModelLogin:

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

    public Object getKey() { 
     return Key; 
    } 

    public Integer getResponse() { 
     return Response; 
    } 
} 

Und RequestInterface_Login:

public interface RequestInterface_Login { 

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

Crash-me:

java.lang.NullPointerException: storage == null 

enter image description here

+0

Bitte setzen Sie den vollen Stacktrace, da das, was Sie gepostet haben, keine Zeile Ihres Codes zeigt. – jakubbialkowski

Antwort

0

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

Verwendung dieser POJO Klasse von oben json .... diese Methode

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; 
} 

} 

und rufen ....

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

Wenn Sie erwarten, dass ein Wert null sein kann, müssen Sie den Objekttyp anstelle von Stirng oder int verwenden. Verwenden Sie die unten angegebene Pojo-Klasse, um die richtige Antwort zu erhalten.

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; 
    } 
}