Advertisement
GeorgePashev_88

interface ContactServerCommunication

Apr 30th, 2024
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. package com.example.kontakti;
  2.  
  3. import okhttp3.OkHttpClient;
  4. import retrofit2.Call;
  5. import retrofit2.Response;
  6. import retrofit2.Retrofit;
  7. import retrofit2.converter.gson.GsonConverterFactory;
  8.  
  9. public interface ContactServerCommunication {
  10.     public interface ShowMessage{
  11.         public void Message(String message);
  12.     }
  13.     public default void APICallInsert(ContactAPI.contacts contact, ShowMessage message){
  14.         Thread t = new Thread(()->{
  15.             try{
  16.                 OkHttpClient client = new OkHttpClient.Builder().build();
  17.                 Retrofit retrofit =
  18.                         new Retrofit.Builder()
  19.                                 .baseUrl("http://10.0.0.13:4041")
  20.                                 .addConverterFactory(
  21.                                         GsonConverterFactory.create()
  22.                                 )
  23.                                 .client(client)
  24.                                 .build();
  25.                 ContactAPI api = retrofit.create(ContactAPI.class);
  26.  
  27.                 Call<ContactAPI.contacts> insertedUser = api.api_add_user(contact);
  28.                 Response<ContactAPI.contacts> r = insertedUser.execute();
  29.                 if(r.isSuccessful()){
  30.                     ContactAPI.contacts resp =r.body();
  31.                     message.Message("INSERTED IN SERVER WITH ID = "+resp.ID);
  32.                 }
  33.  
  34.             }catch (Exception e){
  35.                 message.Message("ERROR FROM SERVER: "+e.getLocalizedMessage());
  36.  
  37.             }
  38.  
  39.         });
  40.         t.start();
  41.  
  42.     }
  43.  
  44.  
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement