我有一个网址,我必须从中下载一个xls文件。我发布编码数据,响应将是一个xls文件。但我不确定如何将此xls文件下载到我的外部存储。当我运行网络电话时,我得到了一个
java.io.FileNotFoundException
它说
Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference
我不知道如何将响应作为文件获取。这是我的代码。
try{
// TODO Auto-generated method stub
String data=URLEncoder.encode("job_id","UTF-8")
+"="+URLEncoder.encode(String.valueOf(responseText),"UTF-8");
data+="&"+URLEncoder.encode("m_id","UTF-8")+"="
+URLEncoder.encode(logedinUserId,"UTF-8");
data+="&"+URLEncoder.encode("start","UTF-8")+"="
+URLEncoder.encode(start,"UTF-8");
data+="&"+URLEncoder.encode("end","UTF-8")+"="
+URLEncoder.encode(endDate,"UTF-8");
Log.e("data",""+data);
String text="";
BufferedReader reader=null;
// Send data
try{
// Defined URL where to send data
URL url=new URL("url here");
// Send POST data request
URLConnection conn=url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr=new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the server response
reader=new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb=new StringBuilder();
String line=null;
// Read Server Response
while((line=reader.readLine())!=null){
// Append server response in string
sb.append(line+"\n");
}
text=sb.toString();
}catch(Exception ex)
{
ex.printStackTrace();
}finally{
try{
reader.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
Log.e("response x",""+text);
}catch(UnsupportedEncodingException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:0)
首先,您必须将下载过程放在 asyncTask 中,以便 UI线程不会被阻止。然后尝试此代码,它将帮助您下载文件。
private class FetchFileURlFromServer extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
onCreateDialog(progress_bar_type);
}
/**
* Downloading file in background thread
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
protected String doInBackground(String... f_url) {
String path = "";
int responseCode = 0;
StringBuilder response = null;
try {
if (f_url[0] != null && !f_url[0].equals(""))
f_url[0] = f_url[0].replace("your url");
URL obj = new URL(f_url[0]);
HttpURLConnection con = (HttpURLConnection)
obj.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);
BufferedReader in = new BufferedReader(new
InputStreamReader(con.getInputStream()));
String inputLine;
response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Response : -- " + response.toString());
// path = connection.getInputStream().toString();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return response != null ? response.toString() : null;
// return path;
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
progressDialogForDownload.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
**/
@Override
protected void onPostExecute(String fileURL) {
// dismiss the dialog after the file was downloaded
if (progressDialogForDownload.isShowing() && progressDialogForDownload != null) {
progressDialogForDownload.dismiss();
progressDialogForDownload = null;
}
// you can open your file using intents or whatever you want to use.
startActivity(browserIntent);
Log.d("", "onButtonClickView: " + fileURL);
}
}
}
希望它对你有所帮助。