Android Retrofit POST

0

I'm having trouble with a program I'm creating, I tried to see some issues here in the stack or on other sites and I could not solve them. I have a connection that generates a list of data that comes from my bank, and I need it as soon as I click the listview long the item that was clicked make a post and be played to my bank, my webservice is already working , I just can not make the codes on android.

public void loadJsonLista(Produto produto){
    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://"+getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString("PrefHost", "") +":8080/FazendaWebservice/webresources/fazenda/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();

    ListaProdutosClient client = retrofit.create(ListaProdutosClient.class);
    Call<Produto> call = client.reposForUser(produto);

    call.enqueue(new Callback<Produto>() {
        @Override
        public void onResponse(Call<Produto> call, Response<Produto> response) {
                Produto produto = response.body();
                Toast.makeText(getApplicationContext(), "Lista criada", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<Produto> call, Throwable t) {
            Toast.makeText(BuscaActivity.this, "Erro ao criar lista", Toast.LENGTH_SHORT).show();
        }
    });
}


listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
        AlertDialog.Builder builder = new AlertDialog.Builder(BuscaActivity.this);
        builder.setTitle("Deseja inserir na lista?");
        builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Produto produto = new Produto();
                loadJsonLista(produto);
            }
        });
        builder.setNegativeButton("Não", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                alerta.cancel();
            }
        });
        alerta = builder.create();
        alerta.show();

        return true;
    }
});

public interface ListaProdutosClient {
    @POST("Produto")
    Call<Produto> reposForUser(@Body Produto produto);
}
    
asked by anonymous 21.09.2017 / 21:35

1 answer

0

Where you create the retrofit request, place a log - >

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor (); interceptor.setLevel (HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder (). AddInterceptor (interceptor) .build ();

So you get the result and see why your server is not receiving it. if you did not put the internet permission put and always see the log. The header of your request on the server should receive an application / json if it is to receive application / x-www-form-urlencoded will not work

    
21.09.2017 / 23:28