Chamar localmente o RESTful Web Service gerado pelo NetBeans IDE do Android?
para que o NetBeans IDE possa gerar RESTful Web Service a partir do banco de dados (a tabela foi compactada em uma classe de entidade). Eu segui issotutorial e o serviço web RESTful foi gerado com sucesso.
Agora, gostaria de ligar do meu aplicativo Android, mas sem sorte até agora. Eu usei "Android Http Client assíncrono - uma biblioteca de cliente Http baseada em retorno de chamada para Android"
Então, aqui está o meu trecho de código:
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// "Done"
String id = generateId();
EditText number = (EditText) findViewById(R.id.caller_phone_number);
EditText information = (EditText) findViewById(R.id.caller_information);
checkAvailability(id, number.getText().toString(), information.getText().toString());
finish();
}
});
e isto:
public void processWebServices(RequestParams params) {
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://localhost:8080/AndroidRESTful/com.erikchenmelbourne.entities.caller/create", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
try {
JSONObject obj = new JSONObject(response.toString());
if (obj.getBoolean("status")) {
setDefaultValues();
Toast.makeText(getApplicationContext(), "Information has been sent!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response is invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
if (i == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
} else if (i == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
}
e aqui está o método POST gerado pelo NetBeans IDE:
@Path("/create")
@POST
@Consumes({"application/xml", "application/json"})
public void create(@QueryParam("id") int id, @QueryParam("number") String number, @QueryParam("information") String information) {
Caller entity = new Caller (id, number, information);
super.create(entity);
}
Adicionei a anotação @Path ("/ create") e modifiquei um pouco o método.
Por favor, mostre alguma luz, eu sou bastante novo nisso, então não tenho idéia. Eu sei que é devido a alguns erros muito tolos, mas por favor me ajude. O programa para em
"Ocorreu um erro inesperado! [Erro mais comum: o dispositivo pode não estar conectado à Internet ou o servidor remoto não está em execução]"
. Tão óbvio que não consegui conectar bem os dois programas.