Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface ApiInterface {
- @Multipart
- @POST("user/login/")
- Call<SessionToken> userLogin(@Part("username") String username, @Part("password") String password);
- }
- username : "brian"
- password : "password"
- username : brian
- password : password
- OkHttpClient client = new OkHttpClient();
- client.interceptors().add(new Interceptor() {
- @Override
- public Response intercept(Chain chain) throws IOException {
- Request original = chain.request();
- // Customize the request
- Request request = original.newBuilder()
- .header("Accept", "application/json")
- .header("Authorization", myPrefs.accessToken().getOr(""))
- .method(original.method(), original.body())
- .build();
- Response response = chain.proceed(request);
- // Customize or return the response
- return response;
- }
- });
- Ok2Curl.set(client);
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(apiEndpoint)
- .addConverterFactory(GsonConverterFactory.create())
- .client(client)
- .build();
- public interface ApiInterface {
- @Multipart
- @POST("user/login/")
- Call<SessionToken> userLogin(@Part("username") RequestBody username, @Part("password") RequestBody password);
- }
- RequestBody usernameBody = RequestBody.create(MediaType.parse("text/plain"), usernameStr);
- RequestBody passwordBody = RequestBody.create(MediaType.parse("text/plain"), passwordStr);
- retrofit.create(ApiInterface.class).userLogin(usernameBody , passwordBody).enqueue()....
- public class StringConverterFactory extends Converter.Factory {
- private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
- public static StringConverterFactory create() {
- return new StringConverterFactory();
- }
- @Override
- public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
- if (String.class.equals(type)) {
- return new Converter<ResponseBody, String>() {
- @Override
- public String convert(ResponseBody value) throws IOException {
- return value.string();
- }
- };
- }
- return null;
- }
- @Override
- public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
- if(String.class.equals(type)) {
- return new Converter<String, RequestBody>() {
- @Override
- public RequestBody convert(String value) throws IOException {
- return RequestBody.create(MEDIA_TYPE, value);
- }
- };
- }
- return null;
- }
- }
- retrofit = new Retrofit.Builder()
- .baseUrl(SERVER_URL)
- .client(client)
- .addConverterFactory(StringConverterFactory.create())
- .addConverterFactory(GsonConverterFactory.create())
- .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
- .build();
- @POST("/children/add")
- Observable<Child> addChild(@Body RequestBody requestBody);
- @Override
- public Observable<Child> addChild(String firstName, String lastName, Long birthDate, @Nullable File passportPicture) {
- MultipartBody.Builder builder = new MultipartBody.Builder()
- .setType(MultipartBody.FORM)
- .addFormDataPart("first_name", firstName)
- .addFormDataPart("last_name", lastName)
- .addFormDataPart("birth_date", birthDate + "");
- //some nullable optional parameter
- if (passportPicture != null) {
- builder.addFormDataPart("certificate", passportPicture.getName(), RequestBody.create(MediaType.parse("image/*"), passportPicture));
- }
- return api.addChild(builder.build());
- }
- RequestBody caption = RequestBody.create(MediaType.parse("text/plain"), new String("caption"));
- return new Retrofit.Builder()
- .baseUrl(Env.GetApiBaseUrl())
- .addConverterFactory(new GsonStringConverterFactory())
- .addConverterFactory(GsonConverterFactory.create(gson))
- .client(getHttpClient())
- .build();
- public class GsonStringConverterFactory extends Converter.Factory {
- private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
- @Override
- public Converter<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) {
- if (String.class.equals(type))// || (type instanceof Class && ((Class<?>) type).isEnum()))
- {
- return new Converter<String, RequestBody>() {
- @Override
- public RequestBody convert(String value) throws IOException {
- return RequestBody.create(MEDIA_TYPE, value);
- }
- };
- }
- return null;
- }
- }
- @Multipart
- @POST("user/login/")
- Call<SessionToken> userLogin(@Part("username") String username, @Part("password") String password);
- @Multipart
- @POST("user/login/")
- Call<SessionToken> userLogin(@Part("username") RequestBody username, @Part("password") String password);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement