【发布时间】:2015-12-16 07:30:48
【问题描述】:
当 URL="http://rest.riob.us/v3/search/474" 时,我的以下代码可以正常工作。
但是对于 URL="http://dadosabertos.rio.rj.gov.br/apiTransporte/apresentacao/rest/index.cfm/onibus/474" 返回 null。
两个 URL 在 webbrowser 上都可以正常工作。
这是我的代码:
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;
}
}
可能出了什么问题?提前谢谢
【问题讨论】:
-
logcat 或 stacktrace
-
logcat for URL="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"}] -
logcat for URL="dadosabertos.rio.rj.gov.br/apiTransporte/apresentacao/rest/…":
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:
标签: android json http-get androidhttpclient