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.
– OnoSendai
I will add yes, in android Studio displays only the 500 message, I will put the error that comes in the Web-Service console.
– Marcos Rai Alves da Cunha
Apparently the error is on the server side and not in the application.
– rubStackOverflow
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.
– Marcos Rai Alves da Cunha
Someone?????????
– Marcos Rai Alves da Cunha