2016-04-24 37 views
1

我想用POST.But登錄一些名爲vid.me,https://api.vid.me/oauth/authorize的服務,但是當我嘗試從日誌中獲取數據時,我有NullPointerException.I嘗試做Toast並且也有這個錯誤。我是試圖獲得響應代碼,看看我做到了這一點或沒有。NPE error using Retrofit

我的API類:

public interface VideoApi { 

    @GET("/videos/featured") 
    Call<Videos> getFeaturedVideo(); 

    @GET("/videos/new") 
    Call<Videos> getNewVideo(); 

    @FormUrlEncoded 
    @POST("oauth/authorize") 
    Call<SignInResults>insertUser(@Field("name") String name, 
          @Field("password") String password 
          ); 
} 

我的片段:

public class FeedFragment extends Fragment { 
    EditText username; 
    EditText password; 
    Button btnLogin; 
    public List<SignInResult> signInResult; 
    public static final String ROOT_URL = "https://api.vid.me/"; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_feed, container, false); 
username = (EditText) rootView.findViewById(R.id.user_name_field); 
     password = (EditText) rootView.findViewById(R.id.password_field); 
     btnLogin = (Button)rootView.findViewById(R.id.button_login); 
     btnLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Authorize(); 
      } 
     }); 
     return rootView; 
    } 
    public void Authorize(){ 
     Retrofit retrofitAdapter = new Retrofit.Builder() 
       .addConverterFactory(GsonConverterFactory.create()) 
       .baseUrl(ROOT_URL) 
       .build(); 
     final VideoApi videoApi = retrofitAdapter.create(VideoApi.class); 
     Call<SignInResults> call = videoApi.insertUser(username.getText().toString(),password.getText().toString()); 
     call.enqueue(new Callback<SignInResults>() { 
      @Override 
      public void onResponse(Call<SignInResults> call, Response<SignInResults> response) { 
       Log.d("FeedFragment", "Status Code = " + response.body().signInResults.get(0).getCode()); 
      } 

      @Override 
      public void onFailure(Call<SignInResults> call, Throwable t) { 

      } 
     }); 

    } 
} 
+0

安置自己的堆棧跟蹤 – jakubbialkowski

+0

這將幫助你。我已經回答了http://stackoverflow.com/questions/36837967/trying-to-get-data-for-post-method-retrofit上的問題 –

回答

0
Call<SignInResponse> call = videoApi.insertUser(username_value, password_value); 
call.enqueue(new Callback<SignInResponse>() { 


    @Override 
    public void onResponse(Call<SignInResponse> call, Response<SignInResponse> response) { 
     SignInResponse results = response.body(); 
     Log.d("Response ==>> ", new GsonBuilder().setPrettyPrinting().create().toJson(results)); 
    } 

    @Override 
    public void onFailure(Call<SignInResponse> call, Throwable t) { 

    } 
}); 



@Headers("Content-Type:application/x-www-form-urlencoded") 
@FormUrlEncoded 
@POST("/auth/create") 
Call<SignInResponse> insertUser(@Field("email") String username, 
          @Field("password") String password 
);