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