2016-04-26 6 views
1

オープンAPIのようなPOSTリクエストをビルドしたいと思っています。私はエラー、悪いリクエスト、コード== 400を持っています。 APIでこの要求の 例:オープンAPIのようにRetrofitを介してリクエスト

curl -X POST 
    -H "Authorization: Basic Zm9vOmJhcg==" 
    -d "username=foo&password=bar" https://api.vid.me/auth/create 

私のインタフェース・クラス:

public interface VideoApi { 

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

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

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

フラグメント:

public class FeedFragment extends Fragment { 
    EditText username; 
    EditText password; 
    Button btnLogin; 

    public List<SignInResult> signInResult; 
    String username_value,password_value; 
    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); 

     username_value = username.getText().toString(); 
     password_value = password.getText().toString(); 
String basicauth = "Basic "+ Base64.encodeToString(String.format("%s:%s",username_value,password_value).getBytes(),Base64.NO_WRAP); 
     Call<SignInResults> call = videoApi.insertUser(basicauth,username_value,password_value); 
     call.enqueue(new Callback<SignInResults>() { 


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

      } 

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

      } 
     }); 
} 
} 

スタックトレース:

rawResponse = {[email protected]} "Response{protocol=http/1.1, code=400, message=Bad Request, url=https://api.vid.me/auth/create}" 
body = {[email protected]} 
cacheControl = null 
cacheResponse = null 
request = {[email protected]} "Request{method=POST, url=https://api.vid.me/auth/create, tag=Request{method=POST, url=https://api.vid.me/auth/create, tag=null}}" 
handshake = {[email protected]} 
headers = {[email protected]} "Content-Type: application/json\nDate: Tue, 26 Apr 2016 07:36:04 GMT\nServer: nginx\nSet-Cookie: rid=58132ed5dd9a405f851d7ba3a05238e5; expires=Sat, 17-May-2031 01:48:34 GMT; Max-Age=475092750; path=/; domain=vid.me\nSet-Cookie: srid=kKmWgypTQSSDdiKprKNUdg-DQiaXQ-IrRIa9riWD48kQG4bK4Mzfbu0fk; expires=Sat, 17-May-2031 01:48:34 GMT; Max-Age=475092750; path=/; domain=vid.me\nX-Request-Time: 12\nX-Vidme-Authorization-Okay: false\nX-Vidme-Server-Id: 79dce5c05de58beae1b751040fd8bcd0\nContent-Length: 85\nConnection: keep-alive\nOkHttp-Sent-Millis: 1461638110857\nOkHttp-Received-Millis: 1461638111114\n" 
message = "Bad Request" 
networkResponse = {[email protected]} "Response{protocol=http/1.1, code=400, message=Bad Request, url=https://api.vid.me/auth/create}" 
priorResponse = null 
protocol = {[email protected]} "http/1.1" 
code = 400 

答えて

1

私は郵便配達員でリクエストをテストしていますが、問題はAndroidではなくあなたのAPIです。エラーは「入力したパスワードは有効ではありません」ですので、なぜパスワードが無効であるのですか?

配達員からこの画面を参照してください: API Request

は素晴らしい一日

+0

を持って、ありがとうございました、あまりにも素晴らしい一日を! –

+0

ユーザー名またはパスワードの例、有効なユーザー名とパスワード、悪い要求を入力しようとしています。このリクエストはアンドロイドで正しく実装されていますか? –

+0

APIがエラーを送信しましたが、ユーザー名とパスワードを送信したため、APIコードでこのエラーが発生した理由を確認してください! API呼び出しを簡素化したい場合は、Retrofitを使用してください:http://square.github.io/retrofit/ – yozzy

関連する問題