SEND AND RECEIVE A LETTER OR NUMBER THROUGH ANDROID RETROFIT

1

I want to send and receive data by Retrofit , but only a specific data, just a letter or a number to perform an action from that ..

private void EnviarDatos(String dato) {
        ConnectivityManager conectivity=(ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = conectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = conectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if(String.valueOf(wifi.getState()).equals ("CONNECTED")||(String.valueOf(mobile.getState()).equals("CONNECTED"))){
            //Toast.makeText(getBaseContext(), "Si hay conexion disponible", Toast.LENGTH_SHORT).show();
            if(wifi.isConnected()||(mobile.isConnected())) {

                Call<Receiver> dat = SmartApiAdapter.getApiService().Receiver(1);
                dat.enqueue(new Callback<Receiver>() {
                    @Override
                    public void onResponse(Call<Receiver> call, Response<Receiver> response) {
                        if(response.isSuccessful()) {
                            Receiver receiver = response.body();
                            String ds = receiver.getDato();
                            puente.obtainMessage(Integer.parseInt(ds));
                            Toast.makeText(getBaseContext(), "Alerta enviada, Mantenga la calma", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Receiver> call, Throwable t) {
                        Toast.makeText(getBaseContext(), "Mensaje no enviado", Toast.LENGTH_LONG).show();
                    }
                });
            }
        }
        else
            Toast.makeText(getBaseContext(), "Sin conexion", Toast.LENGTH_LONG).show();
    }

Here I try to receive a data of Retrofit to then show it in my method handler()

private Handler puente = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            BT2.setText((Integer) msg.obj);
            BT2.setTextColor(BT2.getContext().getResources().getColor(R.color.ROJO));
            //Mostramos el mensage recibido del servidor en pantalla
            //Toast.makeText(getApplicationContext(), (String) msg.obj,
                    //Toast.LENGTH_LONG).show();
        }
    };

And in the case of the shipment I do not understand the procedure to upload, for example, the letter "A" to the server.

    
asked by David Villegas 05.06.2018 в 16:38
source

1 answer

1

First of all I do not know if you will make a POST or GET request, since you have a Receiver class but you will only send and receive a letter. I think that for this case it fits better GET. For the application you need a class and an interface here is an example:

Retrofit request class:

public class webapi {
private static  final String urlBase="http://urlejemplo/servicio/api/";

    final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(20, TimeUnit.SECONDS)
            .writeTimeout(20, TimeUnit.SECONDS)
            .readTimeout(20, TimeUnit.SECONDS)
            .build();

    public Retrofit retrofitPeticion(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(urlBase)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();
        return retrofit;
    }
}

Interface:

public interface interfase {

    @GET("metodoapi")
    Call<String> metodoapi(@Query("letra") String letra);

}

Call:

private void llamada(String letra) {
        interfase service = ws.retrofitPeticion().create(interfase.class);
        Call<String> RespuestaCall = service.metodoapi(letra);
        RespuestaCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                  Log.i("recibido ",response.body().toString());
                } 
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

If I've made a mistake, please let me know;)

    
answered by 05.06.2018 в 20:15