I created a JAX-RS REST Service, with a function that returns me a JsonObject, I can retrieve this information in the browser through the URL , but I can not retrieve it from an android application. How do I correctly configure the Web Service so that I can access the data in my application?
@GET
@Produces("application/json")
public String getJson() {
return "{\"estado\":\"São Paulo \",\"nacionalidade\":\"Brasil \",\"nome\":\"Fulano de Tal \"}";
}
Response in the browser by going to URL :
{"estado":"Acre ","nacionalidade":"Brasil ","nome":"Fulano de Tal "}
Android:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new HttpAsyncTask().execute("http://localhost:8080/Restful/aluno");
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Não funcionou!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
@Override
protected void onPostExecute(String result) {
Log.e("MainActivity", "Tem resultado? "+result.length());
Toast.makeText(getBaseContext(), "Received: \n" + result, Toast.LENGTH_LONG).show();
}
}
In Log the result of the size of this retrieved string is 0, and the result of response is:
Connection to http://localhost:8080/refused
I intend not only to retrieve textual information, I want to retrieve pdfs through this Web Service.