Error in Android connection with Web Service Restfull

1

I'm developing an android app for a course in college. I am connecting the Restfull Web Service with the android using the Gson library to pass data.

In the method below I am performing a POST request, in that I make this connection via web it works normal, however in this method it is presenting the 500 error in the connection.

public class CadastrarMarca extends AppCompatActivity {

HttpURLConnection urlConnection = null;
EditText editTextMarcaCadastro;
Button buttonSalvarMarca;
Marca marca = new Marca();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cadastrar_marca);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    editTextMarcaCadastro = (EditText) findViewById(R.id.editTextMarcaCadastro);
    buttonSalvarMarca = (Button) findViewById(R.id.btnSalvarMarca);
    buttonSalvarMarca.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            marca.setNome(editTextMarcaCadastro.getText().toString());
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL("http://192.168.73.110:8080/MrBar/rest/marca");
                        urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.setRequestProperty("Content-Type", "application/json");
                        urlConnection.setRequestMethod("POST");
                        urlConnection.setRequestProperty("Accept", "application/json");
                        urlConnection.setRequestProperty("charset", "utf-8");
                        urlConnection.setUseCaches(false);
                        urlConnection.setDoOutput(true);
                        urlConnection.setInstanceFollowRedirects(false);
                        urlConnection.connect();
                        System.out.println(urlConnection.getResponseCode() + " Mensagem de sucesso ou falaha na conexao");
                        OutputStream outputStream = urlConnection.getOutputStream();
                        OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream, "UTF-8");


                        Gson gson = new Gson();
                        String jsonMarca = gson.toJson(marca);
                        streamWriter.write(jsonMarca);
                        streamWriter.flush();
                        streamWriter.close();
                        outputStream.close();
                        urlConnection.disconnect();*/
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    });
}

}

Thank you in advance.

Follow the error below

  

SEVERE: Servlet.service () for servlet [br.com.mrbar.util.MrBarResourcesConfig] in context with path [/ MrBar] threw exception [java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity] with root cause   java.lang.IllegalArgumentException: attempt to create saveOrUpdate event with null entity

    
asked by anonymous 07.12.2015 / 21:39

2 answers

2

Download a tool called POSTMAN ( link ) and simulate the call using the method, data, and parameters that you are using in your java code.

If you get the correct answer in POSTMAN, the backend is working as it should and the problem is in your code.

The error you submitted appears to be that the backend (servlet) did not receive the data (null) and triggered an IllegalArgumentException.

I may be wrong (I have not programmed in Java for more than 15 years), but looking at your code I would say that you have data ready to be sent before Connect (), because when you connect to the webserver, all requirements to process the request must be available, and seemingly in your code are coming after and firing the IllegalArgumentException on the server.

Take the test.

    
15.03.2017 / 01:49
0

Frames, are you using hibernate to persist your object on the correct base?

When saving the entity (Persist), it is going null, returning the exception.

If this is not the problem, please post your service / dao code to see what might be causing the exception.

    
30.01.2016 / 03:40