Android Retrofit connect JAVA socket












-1















How to use retrofit for Java server socket connection.
My server having JAVA Server Socket.
and wanted to send request from android using Retrofit.
How to make connection and send - received data over Java socket










share|improve this question


















  • 1





    Retrofit is not designed for sockets.

    – Vladyslav Matviienko
    Nov 22 '18 at 12:20
















-1















How to use retrofit for Java server socket connection.
My server having JAVA Server Socket.
and wanted to send request from android using Retrofit.
How to make connection and send - received data over Java socket










share|improve this question


















  • 1





    Retrofit is not designed for sockets.

    – Vladyslav Matviienko
    Nov 22 '18 at 12:20














-1












-1








-1








How to use retrofit for Java server socket connection.
My server having JAVA Server Socket.
and wanted to send request from android using Retrofit.
How to make connection and send - received data over Java socket










share|improve this question














How to use retrofit for Java server socket connection.
My server having JAVA Server Socket.
and wanted to send request from android using Retrofit.
How to make connection and send - received data over Java socket







java android sockets server retrofit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 11:58









NileshNilesh

6429




6429








  • 1





    Retrofit is not designed for sockets.

    – Vladyslav Matviienko
    Nov 22 '18 at 12:20














  • 1





    Retrofit is not designed for sockets.

    – Vladyslav Matviienko
    Nov 22 '18 at 12:20








1




1





Retrofit is not designed for sockets.

– Vladyslav Matviienko
Nov 22 '18 at 12:20





Retrofit is not designed for sockets.

– Vladyslav Matviienko
Nov 22 '18 at 12:20












1 Answer
1






active

oldest

votes


















0














Here is the way that you can connect to your web service through Retrofit library. You can use this sample to do all ever you want:



1-First you need create a communicator class(to do all send-receive process)



public class Communicator {
private static final String TAG = "Communicator";
private static final String SERVER_URL = "http://127.0.0.1/retrofit";

public void loginPost(String username, String password){

//Here a logging interceptor is created
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);

//The logging interceptor will be added to the http client
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);

//The Retrofit builder will have the client attached, in order to get connection logs
Retrofit retrofit = new Retrofit.Builder()
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(SERVER_URL)
.build(); Interface service = retrofit.create(Interface.class);

Call<ServerResponse> call = service.post("login",username,password);

call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
BusProvider.getInstance().post(new ServerEvent(response.body()));
Log.e(TAG,"Success");
}

@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
// handle execution failures like no internet connectivity
BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
}
});
}

public void loginGet(String username, String password){
//Here a logging interceptor is created
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);

//The logging interceptor will be added to the http client
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);

//The Retrofit builder will have the client attached, in order to get connection logs
Retrofit retrofit = new Retrofit.Builder()
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(SERVER_URL)
.build();

Interface service = retrofit.create(Interface.class);

Call<ServerResponse> call = service.get("login",username,password);

call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
BusProvider.getInstance().post(new ServerEvent(response.body()));
Log.e(TAG,"Success");
}

@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
// handle execution failures like no internet connectivity
BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
}
});
}


both of top methods are used for send username and password to the server for authentication.



2-Second you should use communicator class inside your activity:



public class MainActivity extends AppCompatActivity {

private Communicator communicator;
private String username, password;
private EditText usernameET, passwordET;
private Button loginButtonPost, loginButtonGet;
private TextView information, extraInformation;
private final static String TAG = "MainActivity";
public static Bus bus;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

communicator = new Communicator();

usernameET = (EditText)findViewById(R.id.usernameInput);
passwordET = (EditText)findViewById(R.id.passwordInput);
//This is used to hide the password's EditText characters. So we can avoid the different hint font.
passwordET.setTransformationMethod(new PasswordTransformationMethod());

loginButtonPost = (Button)findViewById(R.id.loginButtonPost);
loginButtonPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username = usernameET.getText().toString();
password = passwordET.getText().toString();
usePost(username, password);
}
});

loginButtonGet = (Button)findViewById(R.id.loginButtonGet);
loginButtonGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username = usernameET.getText().toString();
password = passwordET.getText().toString();
useGet(username, password);
}
});

information = (TextView)findViewById(R.id.information);
extraInformation = (TextView)findViewById(R.id.extraInformation);
}

private void usePost(String username, String password){
communicator.loginPost(username, password);
}

private void useGet(String username, String password){
communicator.loginGet(username, password);
}
}


in the onCreate method we created an instance of communicator class and called useGet and usePost methods to send entered username and password to the server.



3-Don't forget to add library dependencies(add this codes into your build.gradle(Module:app) file:



compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup:otto:1.3.8'
compile 'com.google.code.gson:gson:2.6.2'





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430530%2fandroid-retrofit-connect-java-socket%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Here is the way that you can connect to your web service through Retrofit library. You can use this sample to do all ever you want:



    1-First you need create a communicator class(to do all send-receive process)



    public class Communicator {
    private static final String TAG = "Communicator";
    private static final String SERVER_URL = "http://127.0.0.1/retrofit";

    public void loginPost(String username, String password){

    //Here a logging interceptor is created
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    //The logging interceptor will be added to the http client
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(logging);

    //The Retrofit builder will have the client attached, in order to get connection logs
    Retrofit retrofit = new Retrofit.Builder()
    .client(httpClient.build())
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(SERVER_URL)
    .build(); Interface service = retrofit.create(Interface.class);

    Call<ServerResponse> call = service.post("login",username,password);

    call.enqueue(new Callback<ServerResponse>() {
    @Override
    public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
    BusProvider.getInstance().post(new ServerEvent(response.body()));
    Log.e(TAG,"Success");
    }

    @Override
    public void onFailure(Call<ServerResponse> call, Throwable t) {
    // handle execution failures like no internet connectivity
    BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
    }
    });
    }

    public void loginGet(String username, String password){
    //Here a logging interceptor is created
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    //The logging interceptor will be added to the http client
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(logging);

    //The Retrofit builder will have the client attached, in order to get connection logs
    Retrofit retrofit = new Retrofit.Builder()
    .client(httpClient.build())
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(SERVER_URL)
    .build();

    Interface service = retrofit.create(Interface.class);

    Call<ServerResponse> call = service.get("login",username,password);

    call.enqueue(new Callback<ServerResponse>() {
    @Override
    public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
    BusProvider.getInstance().post(new ServerEvent(response.body()));
    Log.e(TAG,"Success");
    }

    @Override
    public void onFailure(Call<ServerResponse> call, Throwable t) {
    // handle execution failures like no internet connectivity
    BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
    }
    });
    }


    both of top methods are used for send username and password to the server for authentication.



    2-Second you should use communicator class inside your activity:



    public class MainActivity extends AppCompatActivity {

    private Communicator communicator;
    private String username, password;
    private EditText usernameET, passwordET;
    private Button loginButtonPost, loginButtonGet;
    private TextView information, extraInformation;
    private final static String TAG = "MainActivity";
    public static Bus bus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    communicator = new Communicator();

    usernameET = (EditText)findViewById(R.id.usernameInput);
    passwordET = (EditText)findViewById(R.id.passwordInput);
    //This is used to hide the password's EditText characters. So we can avoid the different hint font.
    passwordET.setTransformationMethod(new PasswordTransformationMethod());

    loginButtonPost = (Button)findViewById(R.id.loginButtonPost);
    loginButtonPost.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    username = usernameET.getText().toString();
    password = passwordET.getText().toString();
    usePost(username, password);
    }
    });

    loginButtonGet = (Button)findViewById(R.id.loginButtonGet);
    loginButtonGet.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    username = usernameET.getText().toString();
    password = passwordET.getText().toString();
    useGet(username, password);
    }
    });

    information = (TextView)findViewById(R.id.information);
    extraInformation = (TextView)findViewById(R.id.extraInformation);
    }

    private void usePost(String username, String password){
    communicator.loginPost(username, password);
    }

    private void useGet(String username, String password){
    communicator.loginGet(username, password);
    }
    }


    in the onCreate method we created an instance of communicator class and called useGet and usePost methods to send entered username and password to the server.



    3-Don't forget to add library dependencies(add this codes into your build.gradle(Module:app) file:



    compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup:otto:1.3.8'
    compile 'com.google.code.gson:gson:2.6.2'





    share|improve this answer






























      0














      Here is the way that you can connect to your web service through Retrofit library. You can use this sample to do all ever you want:



      1-First you need create a communicator class(to do all send-receive process)



      public class Communicator {
      private static final String TAG = "Communicator";
      private static final String SERVER_URL = "http://127.0.0.1/retrofit";

      public void loginPost(String username, String password){

      //Here a logging interceptor is created
      HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
      logging.setLevel(HttpLoggingInterceptor.Level.BODY);

      //The logging interceptor will be added to the http client
      OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
      httpClient.addInterceptor(logging);

      //The Retrofit builder will have the client attached, in order to get connection logs
      Retrofit retrofit = new Retrofit.Builder()
      .client(httpClient.build())
      .addConverterFactory(GsonConverterFactory.create())
      .baseUrl(SERVER_URL)
      .build(); Interface service = retrofit.create(Interface.class);

      Call<ServerResponse> call = service.post("login",username,password);

      call.enqueue(new Callback<ServerResponse>() {
      @Override
      public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
      BusProvider.getInstance().post(new ServerEvent(response.body()));
      Log.e(TAG,"Success");
      }

      @Override
      public void onFailure(Call<ServerResponse> call, Throwable t) {
      // handle execution failures like no internet connectivity
      BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
      }
      });
      }

      public void loginGet(String username, String password){
      //Here a logging interceptor is created
      HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
      logging.setLevel(HttpLoggingInterceptor.Level.BODY);

      //The logging interceptor will be added to the http client
      OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
      httpClient.addInterceptor(logging);

      //The Retrofit builder will have the client attached, in order to get connection logs
      Retrofit retrofit = new Retrofit.Builder()
      .client(httpClient.build())
      .addConverterFactory(GsonConverterFactory.create())
      .baseUrl(SERVER_URL)
      .build();

      Interface service = retrofit.create(Interface.class);

      Call<ServerResponse> call = service.get("login",username,password);

      call.enqueue(new Callback<ServerResponse>() {
      @Override
      public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
      BusProvider.getInstance().post(new ServerEvent(response.body()));
      Log.e(TAG,"Success");
      }

      @Override
      public void onFailure(Call<ServerResponse> call, Throwable t) {
      // handle execution failures like no internet connectivity
      BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
      }
      });
      }


      both of top methods are used for send username and password to the server for authentication.



      2-Second you should use communicator class inside your activity:



      public class MainActivity extends AppCompatActivity {

      private Communicator communicator;
      private String username, password;
      private EditText usernameET, passwordET;
      private Button loginButtonPost, loginButtonGet;
      private TextView information, extraInformation;
      private final static String TAG = "MainActivity";
      public static Bus bus;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      communicator = new Communicator();

      usernameET = (EditText)findViewById(R.id.usernameInput);
      passwordET = (EditText)findViewById(R.id.passwordInput);
      //This is used to hide the password's EditText characters. So we can avoid the different hint font.
      passwordET.setTransformationMethod(new PasswordTransformationMethod());

      loginButtonPost = (Button)findViewById(R.id.loginButtonPost);
      loginButtonPost.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      username = usernameET.getText().toString();
      password = passwordET.getText().toString();
      usePost(username, password);
      }
      });

      loginButtonGet = (Button)findViewById(R.id.loginButtonGet);
      loginButtonGet.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      username = usernameET.getText().toString();
      password = passwordET.getText().toString();
      useGet(username, password);
      }
      });

      information = (TextView)findViewById(R.id.information);
      extraInformation = (TextView)findViewById(R.id.extraInformation);
      }

      private void usePost(String username, String password){
      communicator.loginPost(username, password);
      }

      private void useGet(String username, String password){
      communicator.loginGet(username, password);
      }
      }


      in the onCreate method we created an instance of communicator class and called useGet and usePost methods to send entered username and password to the server.



      3-Don't forget to add library dependencies(add this codes into your build.gradle(Module:app) file:



      compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'
      compile 'com.squareup.retrofit2:retrofit:2.1.0'
      compile 'com.squareup.retrofit2:converter-gson:2.1.0'
      compile 'com.squareup:otto:1.3.8'
      compile 'com.google.code.gson:gson:2.6.2'





      share|improve this answer




























        0












        0








        0







        Here is the way that you can connect to your web service through Retrofit library. You can use this sample to do all ever you want:



        1-First you need create a communicator class(to do all send-receive process)



        public class Communicator {
        private static final String TAG = "Communicator";
        private static final String SERVER_URL = "http://127.0.0.1/retrofit";

        public void loginPost(String username, String password){

        //Here a logging interceptor is created
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        //The logging interceptor will be added to the http client
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        //The Retrofit builder will have the client attached, in order to get connection logs
        Retrofit retrofit = new Retrofit.Builder()
        .client(httpClient.build())
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(SERVER_URL)
        .build(); Interface service = retrofit.create(Interface.class);

        Call<ServerResponse> call = service.post("login",username,password);

        call.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
        BusProvider.getInstance().post(new ServerEvent(response.body()));
        Log.e(TAG,"Success");
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {
        // handle execution failures like no internet connectivity
        BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
        }
        });
        }

        public void loginGet(String username, String password){
        //Here a logging interceptor is created
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        //The logging interceptor will be added to the http client
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        //The Retrofit builder will have the client attached, in order to get connection logs
        Retrofit retrofit = new Retrofit.Builder()
        .client(httpClient.build())
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(SERVER_URL)
        .build();

        Interface service = retrofit.create(Interface.class);

        Call<ServerResponse> call = service.get("login",username,password);

        call.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
        BusProvider.getInstance().post(new ServerEvent(response.body()));
        Log.e(TAG,"Success");
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {
        // handle execution failures like no internet connectivity
        BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
        }
        });
        }


        both of top methods are used for send username and password to the server for authentication.



        2-Second you should use communicator class inside your activity:



        public class MainActivity extends AppCompatActivity {

        private Communicator communicator;
        private String username, password;
        private EditText usernameET, passwordET;
        private Button loginButtonPost, loginButtonGet;
        private TextView information, extraInformation;
        private final static String TAG = "MainActivity";
        public static Bus bus;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        communicator = new Communicator();

        usernameET = (EditText)findViewById(R.id.usernameInput);
        passwordET = (EditText)findViewById(R.id.passwordInput);
        //This is used to hide the password's EditText characters. So we can avoid the different hint font.
        passwordET.setTransformationMethod(new PasswordTransformationMethod());

        loginButtonPost = (Button)findViewById(R.id.loginButtonPost);
        loginButtonPost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        username = usernameET.getText().toString();
        password = passwordET.getText().toString();
        usePost(username, password);
        }
        });

        loginButtonGet = (Button)findViewById(R.id.loginButtonGet);
        loginButtonGet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        username = usernameET.getText().toString();
        password = passwordET.getText().toString();
        useGet(username, password);
        }
        });

        information = (TextView)findViewById(R.id.information);
        extraInformation = (TextView)findViewById(R.id.extraInformation);
        }

        private void usePost(String username, String password){
        communicator.loginPost(username, password);
        }

        private void useGet(String username, String password){
        communicator.loginGet(username, password);
        }
        }


        in the onCreate method we created an instance of communicator class and called useGet and usePost methods to send entered username and password to the server.



        3-Don't forget to add library dependencies(add this codes into your build.gradle(Module:app) file:



        compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'
        compile 'com.squareup.retrofit2:retrofit:2.1.0'
        compile 'com.squareup.retrofit2:converter-gson:2.1.0'
        compile 'com.squareup:otto:1.3.8'
        compile 'com.google.code.gson:gson:2.6.2'





        share|improve this answer















        Here is the way that you can connect to your web service through Retrofit library. You can use this sample to do all ever you want:



        1-First you need create a communicator class(to do all send-receive process)



        public class Communicator {
        private static final String TAG = "Communicator";
        private static final String SERVER_URL = "http://127.0.0.1/retrofit";

        public void loginPost(String username, String password){

        //Here a logging interceptor is created
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        //The logging interceptor will be added to the http client
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        //The Retrofit builder will have the client attached, in order to get connection logs
        Retrofit retrofit = new Retrofit.Builder()
        .client(httpClient.build())
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(SERVER_URL)
        .build(); Interface service = retrofit.create(Interface.class);

        Call<ServerResponse> call = service.post("login",username,password);

        call.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
        BusProvider.getInstance().post(new ServerEvent(response.body()));
        Log.e(TAG,"Success");
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {
        // handle execution failures like no internet connectivity
        BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
        }
        });
        }

        public void loginGet(String username, String password){
        //Here a logging interceptor is created
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        //The logging interceptor will be added to the http client
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        //The Retrofit builder will have the client attached, in order to get connection logs
        Retrofit retrofit = new Retrofit.Builder()
        .client(httpClient.build())
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(SERVER_URL)
        .build();

        Interface service = retrofit.create(Interface.class);

        Call<ServerResponse> call = service.get("login",username,password);

        call.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
        BusProvider.getInstance().post(new ServerEvent(response.body()));
        Log.e(TAG,"Success");
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {
        // handle execution failures like no internet connectivity
        BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
        }
        });
        }


        both of top methods are used for send username and password to the server for authentication.



        2-Second you should use communicator class inside your activity:



        public class MainActivity extends AppCompatActivity {

        private Communicator communicator;
        private String username, password;
        private EditText usernameET, passwordET;
        private Button loginButtonPost, loginButtonGet;
        private TextView information, extraInformation;
        private final static String TAG = "MainActivity";
        public static Bus bus;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        communicator = new Communicator();

        usernameET = (EditText)findViewById(R.id.usernameInput);
        passwordET = (EditText)findViewById(R.id.passwordInput);
        //This is used to hide the password's EditText characters. So we can avoid the different hint font.
        passwordET.setTransformationMethod(new PasswordTransformationMethod());

        loginButtonPost = (Button)findViewById(R.id.loginButtonPost);
        loginButtonPost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        username = usernameET.getText().toString();
        password = passwordET.getText().toString();
        usePost(username, password);
        }
        });

        loginButtonGet = (Button)findViewById(R.id.loginButtonGet);
        loginButtonGet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        username = usernameET.getText().toString();
        password = passwordET.getText().toString();
        useGet(username, password);
        }
        });

        information = (TextView)findViewById(R.id.information);
        extraInformation = (TextView)findViewById(R.id.extraInformation);
        }

        private void usePost(String username, String password){
        communicator.loginPost(username, password);
        }

        private void useGet(String username, String password){
        communicator.loginGet(username, password);
        }
        }


        in the onCreate method we created an instance of communicator class and called useGet and usePost methods to send entered username and password to the server.



        3-Don't forget to add library dependencies(add this codes into your build.gradle(Module:app) file:



        compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'
        compile 'com.squareup.retrofit2:retrofit:2.1.0'
        compile 'com.squareup.retrofit2:converter-gson:2.1.0'
        compile 'com.squareup:otto:1.3.8'
        compile 'com.google.code.gson:gson:2.6.2'






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 14:33









        blackeyedcenter

        697




        697










        answered Nov 22 '18 at 12:27









        mdevelopermdeveloper

        63




        63






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430530%2fandroid-retrofit-connect-java-socket%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'