-1

My Code below is working properly when URL="http://rest.riob.us/v3/search/474".

However is returning null for URL="http://dadosabertos.rio.rj.gov.br/apiTransporte/apresentacao/rest/index.cfm/onibus/474".

Both URLs are working fine on webbrowser.

Here is my code:

public class HttpGetAsyncTask  extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {

    return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {

    Log.w("TAG", "result: " + result);
}

public static String GET(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        Log.v("TAG", "inputStream: " + inputStream);
        Log.e("TAG", "httpResponse.getEntity().getContentLength(): " + httpResponse.getEntity().getContentLength());

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("TAG", 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;

}

}

What could be wrong? Tks in advance

kbeca
  • 29
  • 4
  • logcat or stacktrace – njzk2 Sep 19 '15 at 04:11
  • logcat for URL="http://rest.riob.us/v3/search/474": `09-19 10:52:17.684: V/TAG(700): inputStream: org.apache.http.conn.EofSensorInputStream@410af718 09-19 10:52:17.684: E/TAG(700): httpResponse.getEntity().getContentLength(): 3238 09-19 10:52:20.382: W/TAG(700): result: [{"line":"474","order":"C47499","speed":55,"direction":271,"latitude":-22.999784,"longitude":-43.35051,"sense":"CARIOCA (VIA ESTRADA BENVINDO DE NOVAES) X PIABAS","timeStamp":"2015-09-19T13:50:38.000Z"}]` – kbeca Sep 19 '15 at 14:05
  • logcat for URL="http://dadosabertos.rio.rj.gov.br/apiTransporte/apresentacao/rest/index.cfm/onibus/474": `09-19 11:10:39.143: V/TAG(757): inputStream: org.apache.http.conn.EofSensorInputStream@410f4b90 09-19 11:10:39.152: E/TAG(757): httpResponse.getEntity().getContentLength(): -1 09-19 11:10:44.392: W/TAG(757): result:` – kbeca Sep 19 '15 at 14:13

1 Answers1

0

If you are doing network requests for an API or JSON, look into the VOLLEY library. It is super simple to use and it handles the queueing, async and caching for you. Example:

RequestQueue queue = Volley.newRequestQueue(this);

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            //DO WITH RESPONSE
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //CRAP ERROR
            Log.e("Volley", error.toString());
        }
    });
queue.add(request);

Seriously If you are going to request for JSON data use Volley its so much simpler! This is all the code you would need for a JSON request, it also offers other built in request types for Images or Strings and even allows custom types.

  • But *why* is the code acting the way it is? (Where) is this behavior documented? How can it be addressed in the *existing* code? One should always try to answer the question, even when including a (relevant) aside. – user2864740 Sep 19 '15 at 04:19
  • I'm not gonna spend time debugging crappy code that someone else wrote when there is an excellent library that does web requests for you. No one wastes time writing HttpClient calls, unless they have to, which I have had to do for custom file transfers. Debugging the code would just be promoting bad development and a waste of time. – Anthony Tatowicz Sep 19 '15 at 04:25
  • 1
    That's fine. I'm not going to either. There are many more worthwhile questions to answer. However, answers *should* be answers. – user2864740 Sep 19 '15 at 04:26