Error on Android connection with Restfull Web Service

Asked

Viewed 366 times

1

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

In the method below I am performing a POST request, in what I perform this connection via web it works normal, however in this method it is presenting error 500 in 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

  • Could you add the error message? This should help detect the real problem. 500 is generic HTTP code for server-side errors.

  • 1

    I will add yes, in android Studio displays only the 500 message, I will put the error that comes in the Web-Service console.

  • 1

    Apparently the error is on the server side and not in the application.

  • Apparently from what I noticed the object is not being recorded because it is null, ie it is not being passed in a correct way, I believe.

  • Someone?????????

2 answers

2

Download a tool called POSTMAN (https://www.getpostman.com) and simulate the call using the method, data and parameters 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 sent appears to be that the backend (Servlet) did not receive the data(null) and fired an Illegalargumentexception.

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

Take the test.

0

Marcos, you are using Ibernate to persist your object on the correct basis?

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

If this is not the problem, put the code of your service/dao to verify what may be the cause of the exception.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.