Мой код ниже работает правильно, когда URL = "http://rest.riob.us/v3/search/474".HttpGet возвращает null для определенного URL-адреса
Однако возвращает null для URL = "http://dadosabertos.rio.rj.gov.br/apiTransporte/apresentacao/rest/index.cfm/onibus/474".
Оба URL-адреса отлично работают на веб-браузере.
Вот мой код:
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;
}
}
Что может быть не так? Tks заранее
logcat или stacktrace – njzk2
logcat для URL = "http://rest.riob.us/v3/search/474": '09-19 10: 52: 17.684: V/TAG (700): inputStream: org. [email protected] 09-19 10: 52: 17.684: E/TAG (700): httpResponse.getEntity(). getContentLength(): 3238 09-19 10: 52: 20.382: W/TAG (700): результат: [{«строка»: «474», «порядок»: «C47499», «скорость»: 55, «направление»: 271, «широта»: - 22.999784, «долгота»: - 43.35051, «смысл»: «CARIOCA (VIA ESTRADA BENVINDO DE NOVAES) X PIABAS», «TimeStamp»: «2015-09-19T13: 50: 38.000Z»}] ' – kbeca
logcat для 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(). G etContentLength(): -1 09-19 11: 10: 44.392: W/TAG (757): result: ' – kbeca