1
I created a JAX-RS REST Service, with a function that returns me a Jsonobject, I can recover this information in the browser through the URL, but I can’t get it back on an android app. How do I properly configure the Web Service so 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 \"}";
}
Browser response by accessing 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 recovered 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 recover pdfs through this Web Service.
Have you tried using the IP/domain of the machine you are running WS on instead of
localhost?– Bruno César
That was it @Bruno César I was able to recover, thank you. But regarding the pdf, what is the best way to recover it in the app and store it?
– Reiko Dev
Have you tried anything, how to implement the server service? Something in the way to recover in the app or are still looking for ways?
– Bruno César