Java BufferedReader读取不完整的文件内容
我正在尝试解析一些JSON数据,这些数据是作为web url的响应接收的 我得到了完整的响应,但是当我使用BufferedReader读取响应时,我只得到了部分数据,我无法读取响应的全部内容 我还尝试将内容写入.txt文件,然后再次从该文件中读取内容,但它仍然只读取部分dat,而不是整个文件内容 任何人都有解决这个问题的办法 这是我的密码Java BufferedReader读取不完整的文件内容,java,android,json,url,bufferedreader,Java,Android,Json,Url,Bufferedreader,我正在尝试解析一些JSON数据,这些数据是作为web url的响应接收的 我得到了完整的响应,但是当我使用BufferedReader读取响应时,我只得到了部分数据,我无法读取响应的全部内容 我还尝试将内容写入.txt文件,然后再次从该文件中读取内容,但它仍然只读取部分dat,而不是整个文件内容 任何人都有解决这个问题的办法 这是我的密码 package com.example.asynctest; /*I have excluded the imports*/ public
package com.example.asynctest;
/*I have excluded the imports*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyAsyncTask().execute();
}
class MyAsyncTask extends AsyncTask<String, String, Void>{
public String readBugzilla() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("https://bugzilla.mozilla.org/rest/bug?assigned_to=lhenry@mozilla.com");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
/*I tried to Read the InputStream here, but it was incomplete, so I decided to write the contents of the inputstream into a text file, and then read the contents later on*/
String fileName = "category.txt";
File destinationFile = new File(getExternalFilesDir(null), fileName);
BufferedOutputStream buffer = new BufferedOutputStream(new FileOutputStream(destinationFile));
byte byt[] = new byte[100000];
int i;
for (long l = 0L; (i = content.read(byt)) != -1; l += i ) {
buffer.write(byt, 0, i);
}
buffer.close();
//Read text from file
File file = new File(getExternalFilesDir(null),"category.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
Log.d("Law", "line is "+line);
}
br.close();
Log.d("Law","Text is "+text.toString());
}
catch (IOException e) {
//You'll need to add proper error handling here
Log.d("Law IO", e.toString());
}
} else {
Log.e("Law", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected Void doInBackground(String... arg0) {
// TODO Auto-generated method stub
String response = readBugzilla();
Log.d("Law", "Response "+response);
return null;
}
package com.example.asynctest;
/*我已排除进口货物*/
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建MyAsyncTask().execute();
}
类MyAsyncTask扩展了AsyncTask{
公共字符串readBugzilla(){
StringBuilder=新的StringBuilder();
HttpClient=new DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(“https://bugzilla.mozilla.org/rest/bug?assigned_to=lhenry@mozilla.com),;
试一试{
HttpResponse response=client.execute(httpGet);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
如果(状态代码==200){
HttpEntity=response.getEntity();
InputStream内容=entity.getContent();
/*我试图在这里读取InputStream,但它不完整,所以我决定将InputStream的内容写入一个文本文件,然后稍后读取内容*/
字符串fileName=“category.txt”;
File destinationFile=新文件(getExternalFilesDir(null),文件名);
BufferedOutputStream buffer=新的BufferedOutputStream(新文件输出流(destinationFile));
字节byt[]=新字节[100000];
int i;
对于(长l=0L;(i=content.read(byt))!=-1;l+=i){
写缓冲区(byt,0,i);
}
buffer.close();
//从文件中读取文本
File File=新文件(getExternalFilesDir(null),“category.txt”);
StringBuilder text=新的StringBuilder();
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(文件));
弦线;
而((line=br.readLine())!=null){
text.append(行);
Log.d(“法律”,“行是”+行);
}
br.close();
Log.d(“Law”,“Text is”+Text.toString());
}
捕获(IOE异常){
//您需要在此处添加正确的错误处理
Log.d(“Law IO”,例如toString());
}
}否则{
Log.e(“法律”,“未能下载文件”);
}
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回builder.toString();
}
@凌驾
受保护的Void doInBackground(字符串…arg0){
//TODO自动生成的方法存根
字符串响应=readBugzilla();
日志d(“法律”、“回应”+回应);
返回null;
}
我真的尽了我所能,但找不到解决这个问题的方法,也许有人使用了更好的技术来解决这个问题。…或者这是logcat的一个限制…在关闭之前检查PCTry
buffler.flush()
上的结果文件该文件有多大?占总数的%)你认为它最终会读取吗?@Neo文件大小约为65 kb,但我只能读取整个内容的15%。@Selvin是对的!我将BufferedReader的内容写入了TextView,并能够查看所有JSON数据。非常感谢!!