想知道是否有人有任何洞察力,为什么这不起作用。首先代码去并获得一个链接到一个pdf在一个网站上,并链接到PDF是绝对正确,但它似乎并没有下载到我的SD卡上。不知道如果它是从我的设备上的Android工作室运行它的方式完成的?链接只是一个容器对象我有URL用一些方法给了刚刚的文件名,整个URL的PDF等下载文件到sd卡不工作
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnFetchData = (Button) findViewById(R.id.buttonTest);
btnFetchData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new FetchWebsiteData().execute();
}
});
}
private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {
private String pdfLink = "didnt work";
private Link foundLink = new Link("");
String fileLocation= "";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
int count;
try {
Document doc = Jsoup.connect("http://www.dunnesstores.com/offer20/food-wine/fcp-category/home").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0").get();
//Elements links = doc.select("a[title=\"Download offers in store\"]");
Element links = doc.select("a[title=\"Download offers in store\"]").first();
foundLink = new Link(links.attr("href"));
URL url = new URL(foundLink.getUrlWithDomain());
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot,foundLink.getFileNameOnly());
fileLocation = file.toString();
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ((bufferLength = inputStream.read(buffer)) > 0) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the progress, like this maybe
}
//close the output stream when done
fileOutput.close();
} catch (IOException e) {
//e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
TextView txttitle = (TextView) findViewById(R.id.resultTextView);
//testing the file location
txttitle.setText(fileLocation);
}
}
在我的清单
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
的权限
编辑:发现的异常堆栈跟踪这应有助于java.io.FileNotFoundException如下:/storage/emulated/0/29-11-16.pdf:打开失败:EACCES(拒绝)
你得到任何错误讯息?如果您从网站收到数据,您是否登录过?首先检查下载数据或写入文件是否导致问题。 – MrSmith42
'catch(IOException e){0e.printStackTrace(); }'。让堆栈跟踪打印!然后返回你在onPostExecute中检查的e.getMessage()。现在你什么都不知道! – greenapps
'onPostExecute(Void result)'。目前'result'总是'null',doInBoackground总是返回'null'; – greenapps