“Котлин и модернизатор” Ответ

Модернизация зависимости в Android Studio Kotlin

// Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.6.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
    implementation "com.squareup.okhttp3:logging-interceptor:4.5.0"
Ibk

Котлин и модернизатор

- intoduction
        1. post is used to send data to server

- exapmle
   "simple post"
            @POST("posts")
            Call<Post> createPost(@Body Post post);

   "post with from url encoded "
            @FormUrlEncoded
            @POST("posts")
            Call<Post> createPost( @Field("userId") int userid ,
                                    @Field("title") String title,
                                    @Field("body") String body );

    "using map"
            @FormUrlEncoded
            @POST("posts")
            Call<Post> createPost(@FieldMap Map<String,String> postMap);







- INTERFACE
        public interface MyWebService {

            String BASE_URL = "https://jsonplaceholder.typicode.com/";
            String FEED = "posts";
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        
            @GET(FEED)
            Call<List<Post>> getPosts();
        
        
        }



        
- MODEL CLASS
       public class Post {

            private int userId;
            private int id;
            private String title;
            private String body;
        
            public int getUserId() {
                return userId;
            }
        
            public void setUserId(int userId) {
                this.userId = userId;
            }
        
            public int getId() {
                return id;
            }
        
            public void setId(int id) {
                this.id = id;
            }
        
            public String getTitle() {
                return title;
            }
        
            public void setTitle(String title) {
                this.title = title;
            }
        
            public String getBody() {
                return body;
            }
        
            public void setBody(String body) {
                this.body = body;
            }
        
       }
        
        
        


- IN ACTIVITY WHERE TO SEND POST REQUEST
        Post post = new Post(1,"Post Title","this is post body");

        Map<String,String> postmap = new HashMap<>();
        postmap.put("userId","33");
        postmap.put("title","My post title");
        postmap.put("body","this is my post body in the map");
    
        Call<Post> postCall = myWebService.createPost(postmap);
        postCall.enqueue(new Callback<Post>() {
            @Override
            public void onResponse(Call<Post> call, Response<Post> response) {
                if (response.isSuccessful()){
                    textView.setText(String.valueOf(response.code()));
                    show(response.body());
                }
            }
    
            @Override
            public void onFailure(Call<Post> call, Throwable t) {
    
            }
        });
android developer

Ответы похожие на “Котлин и модернизатор”

Вопросы похожие на “Котлин и модернизатор”

Больше похожих ответов на “Котлин и модернизатор” по Kotlin

Смотреть популярные ответы по языку

Смотреть другие языки программирования