您好,我正在從Android設備的URL中接收JSON文件。我不知道爲什麼我只得到JSON的第一個字符,這是一個「{」而不是 {"status":"OK","num_results":5,"results":[{"id":1,"title":"Apartamentos Campus"
, 然後is.available()始終返回1。 有趣的是,如果我在另一臺設備上使用它,它會收到一切。從網址收到問題
我正在使用wifi連接。
且本人得到的輸出爲:
03-11 13:12:17.377:VERBOSE/Mixare(15816):attemps:1
03-11 13:12:17.382:VERBOSE/Mixare(15816):is.available():1
03-11 13:12:17.462:VERBOSE/Mixare(15816):attemps:1
03-11 13:12:17.482:VERBOSE/Mixare(15816):is.available():1
0 3-11 13:12:19.417:VERBOSE/Mixare(15816):企圖:2
03-11 13:12:19.417:VERBOSE/Mixare(15816):is.available():1 ...
這是代碼:
public InputStream getHttpGETInputStream(String urlStr, int attemps) throws Exception {
URL u = new URL(urlStr);
InputStream is = null;
try{
URLConnection uc = u.openConnection();
is = uc.getInputStream();
}
catch(Exception e){
Log.v(MixView.TAG, "Excepcion leyendo inputStream: "+e.getMessage());
}
Log.v(MixView.TAG, "attemps: "+attemps);
Log.v(MixView.TAG, "is.available(): "+is.available());
if(is.available() < 50 && attemps < 6){
try {
Thread.sleep(2000);//para y lo vuelve a intentar descargar
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
attemps++;
is.close();
return getHttpGETInputStream(urlStr,attemps);
}
return is;
}
'available()'只返回當前緩衝的字節數量,而不是流的總長度。你可以包括你從流中讀取的實際代碼嗎? – 2011-03-11 13:36:10