Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. public interface ApiInterface {
  2.  
  3. @Multipart
  4. @POST("user/login/")
  5. Call<SessionToken> userLogin(@Part("username") String username, @Part("password") String password);
  6. }
  7.  
  8. username : "brian"
  9. password : "password"
  10.  
  11. username : brian
  12. password : password
  13.  
  14. OkHttpClient client = new OkHttpClient();
  15. client.interceptors().add(new Interceptor() {
  16. @Override
  17. public Response intercept(Chain chain) throws IOException {
  18. Request original = chain.request();
  19.  
  20. // Customize the request
  21. Request request = original.newBuilder()
  22. .header("Accept", "application/json")
  23. .header("Authorization", myPrefs.accessToken().getOr(""))
  24. .method(original.method(), original.body())
  25. .build();
  26.  
  27. Response response = chain.proceed(request);
  28.  
  29. // Customize or return the response
  30. return response;
  31. }
  32. });
  33.  
  34. Ok2Curl.set(client);
  35.  
  36. Retrofit retrofit = new Retrofit.Builder()
  37. .baseUrl(apiEndpoint)
  38. .addConverterFactory(GsonConverterFactory.create())
  39. .client(client)
  40. .build();
  41.  
  42. public interface ApiInterface {
  43. @Multipart
  44. @POST("user/login/")
  45. Call<SessionToken> userLogin(@Part("username") RequestBody username, @Part("password") RequestBody password);
  46. }
  47.  
  48. RequestBody usernameBody = RequestBody.create(MediaType.parse("text/plain"), usernameStr);
  49. RequestBody passwordBody = RequestBody.create(MediaType.parse("text/plain"), passwordStr);
  50.  
  51. retrofit.create(ApiInterface.class).userLogin(usernameBody , passwordBody).enqueue()....
  52.  
  53. public class StringConverterFactory extends Converter.Factory {
  54. private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
  55.  
  56. public static StringConverterFactory create() {
  57. return new StringConverterFactory();
  58. }
  59.  
  60. @Override
  61. public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
  62. if (String.class.equals(type)) {
  63. return new Converter<ResponseBody, String>() {
  64.  
  65. @Override
  66. public String convert(ResponseBody value) throws IOException {
  67. return value.string();
  68. }
  69. };
  70. }
  71. return null;
  72. }
  73.  
  74. @Override
  75. public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
  76. if(String.class.equals(type)) {
  77. return new Converter<String, RequestBody>() {
  78. @Override
  79. public RequestBody convert(String value) throws IOException {
  80. return RequestBody.create(MEDIA_TYPE, value);
  81. }
  82. };
  83. }
  84.  
  85. return null;
  86. }
  87.  
  88. }
  89.  
  90. retrofit = new Retrofit.Builder()
  91. .baseUrl(SERVER_URL)
  92. .client(client)
  93. .addConverterFactory(StringConverterFactory.create())
  94. .addConverterFactory(GsonConverterFactory.create())
  95. .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
  96. .build();
  97.  
  98. @POST("/children/add")
  99. Observable<Child> addChild(@Body RequestBody requestBody);
  100.  
  101. @Override
  102. public Observable<Child> addChild(String firstName, String lastName, Long birthDate, @Nullable File passportPicture) {
  103. MultipartBody.Builder builder = new MultipartBody.Builder()
  104. .setType(MultipartBody.FORM)
  105. .addFormDataPart("first_name", firstName)
  106. .addFormDataPart("last_name", lastName)
  107. .addFormDataPart("birth_date", birthDate + "");
  108.  
  109. //some nullable optional parameter
  110. if (passportPicture != null) {
  111. builder.addFormDataPart("certificate", passportPicture.getName(), RequestBody.create(MediaType.parse("image/*"), passportPicture));
  112. }
  113. return api.addChild(builder.build());
  114. }
  115.  
  116. RequestBody caption = RequestBody.create(MediaType.parse("text/plain"), new String("caption"));
  117.  
  118. return new Retrofit.Builder()
  119. .baseUrl(Env.GetApiBaseUrl())
  120. .addConverterFactory(new GsonStringConverterFactory())
  121. .addConverterFactory(GsonConverterFactory.create(gson))
  122. .client(getHttpClient())
  123. .build();
  124.  
  125. public class GsonStringConverterFactory extends Converter.Factory {
  126. private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
  127.  
  128. @Override
  129. public Converter<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) {
  130. if (String.class.equals(type))// || (type instanceof Class && ((Class<?>) type).isEnum()))
  131. {
  132. return new Converter<String, RequestBody>() {
  133. @Override
  134. public RequestBody convert(String value) throws IOException {
  135. return RequestBody.create(MEDIA_TYPE, value);
  136. }
  137. };
  138. }
  139. return null;
  140. }
  141. }
  142.  
  143. @Multipart
  144. @POST("user/login/")
  145. Call<SessionToken> userLogin(@Part("username") String username, @Part("password") String password);
  146.  
  147. @Multipart
  148. @POST("user/login/")
  149. Call<SessionToken> userLogin(@Part("username") RequestBody username, @Part("password") String password);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement