私はWebサーバーに接続し、応答を受け取るアプリケーションで作業しています。私はこの応答をデバイスの内部メモリのtxtファイルに保存しています。今私はファイル全体を読むことを試みればそれはOutofMemoryException
を投げているので、行ごとにファイル全体を読む必要があります。Androidは大きなファイルを行単位で読み込みます
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.rpc.probnata.com");
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("debug_data","1"));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);
Log.w("Response ","Status line : "+ response.getStatusLine().toString());
buffer = EntityUtils.toByteArray(response.getEntity());
FileOutputStream out = this.openFileOutput("response",this.MODE_PRIVATE);
out.write(buffer);
out.close();
ファイルの読み込み:
public void parseResponse(){
try {
File myDir = new File(getFilesDir().getAbsolutePath());
BufferedReader br = new BufferedReader(new FileReader(myDir + "/response"));
String line;
while ((line = br.readLine()) != null) {
Log.v("","line : "+line);
handleDataFromSync(line);
}
br.close();
} catch (Exception e){
e.printStackTrace();
}
}
このメソッドは、応答を解析するので、今のところ、私はこのコードを使用してファイルを書き込み、読んでいます。
public void handleDataFromSync(final String responseBody) {
for(int index=0;index<responseBody.length();index++){
Log.w("Response ","Response size : "+ responseBody.length());
Log.w("","****************Index is : "+index);
int objectIdentificator = 0;
objectIdentificator = Integer.parseInt(responseBody.substring(index,index+packetFieldSizes[0]));
Log.w("Response ","Object Identificator (LONGINT) : "+ objectIdentificator);
index = index+packetFieldSizes[0];
Log.w("","****************Index is (must be 32) : "+index);
String type = null;
type = responseBody.substring(index,index + packetFieldSizes[1]);
Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ type);
short pType = Short.parseShort(type);
Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ pType);
index = index + packetFieldSizes[1];
Log.w("","****************Index is (must be 35) : "+index);
String operation=null;
operation = responseBody.substring(index,index + packetFieldSizes[2]);
short operationType = Short.parseShort(operation);
Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operation);
Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operationType);
index = index + packetFieldSizes[2];
Log.w("","****************Index is (must be 38) : "+index);
String objectId=null;
objectId = responseBody.substring(index, index + packetFieldSizes[3]);
Log.w("Response ","UID (CHAR, length 32) : "+ objectId);
index = index + packetFieldSizes[3];
Log.w("","****************Index is (must be 70) : "+index);
int id=0;
id = Integer.parseInt(responseBody.substring(index,index + packetFieldSizes[4]));
Log.w("Response ","ID (LONGINT) : "+ responseBody.substring(index, index + packetFieldSizes[4]));
Log.w("Response ","ID (LONGINT) : "+ id);
index = index + packetFieldSizes[4];
Log.w("","****************Index is (must be 102) : "+index);
String size=null;
size = responseBody.substring(index,index + packetFieldSizes[5]);
int dataSize = Integer.parseInt(size);
Log.w("Response ","Data Size (LONGINT) : "+ dataSize);
index = index + packetFieldSizes[5];
Log.w("","****************Index is (must be 134) : "+index);
String hash=null;
hash = responseBody.substring(index,index + packetFieldSizes[6]);
Log.w("Response ","Data Hash (CHAR, length 32 : "+ hash);
index = index + packetFieldSizes[6];
Log.w("","****************Index is (must be 166) : "+index);
String dType=null;
dType = responseBody.substring(index,index + packetFieldSizes[7]);
Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dType);
short dataType = Short.parseShort(dType);
Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dataType);
index = index + packetFieldSizes[7];
Log.w("","****************Index is (must be 169) : "+index);
String data=null;
data = responseBody.substring(index, index + dataSize);
Log.w("Response ","Data (CHAR, any length, in BASE64) : "+ data);
index = (index + dataSize)-1;
Log.w("","****************Index is must be : "+index);
byte[] first = Base64.decode(data);
String string = new String(first, "UTF-8");
Log.w("Response ","BASE 64 : "+ string);
}
}
ので、このコードは、今やっている事は、最初の行を読み取ることで、その後、再び最初の行を読み取るtryintだから、次の行を読み取る方法任意のアイデア。
実際に問題がファイルを読み取っていることがわかりました。私の応答が30K未満の場合は、応答の最後に30Kに達する文字がいくつか追加され、例外が発生しています。答えをありがとう!あなたが私の質問に答える唯一の人なので、私はそれを受け入れたものとしてマークします:)ありがとう! –
うれしいことに、あなたはそれがうまくいって、爆笑! – Carth