왜 이것이 작동하지 않는지에 대한 통찰력이 있는지 궁금합니다. 먼저 모든 코드가 웹 사이트에서 pdf로 연결되는 링크를 얻습니다. pdf에 대한 링크는 다음과 같습니다. 확실히 올바른,하지만 내 SD 카드에 그것을 다운로드하지 않는 것 같습니다. 확실하지 않은 경우 그것은 내 장치에서 안드로이드 스튜디오에서 그것을 실행하는 방식으로 완료되면? 링크는 그냥 컨테이너 개체입니다 나는 URL을 가지고 단지 파일 이름, 전체 URL을주는 몇 가지 방법과 PDF, 등 내 매니페스트SD 카드에 파일을 다운로드하지 못했습니다.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
,369에서
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);
}
}
권한
편집 : java.io.FileNotFoundException 도움이 될 예외 스택 추적에서 다음을 찾았습니다. /storage/emulated/0/29-11-16.pdf : 열기 실패 : EACCES (사용 권한이 거부 됨)
같이 사용할 수있다? 웹 사이트에서 데이터를 받으면 로그인 했습니까? 먼저 데이터를 다운로드하거나 파일에 쓰면 문제가 발생하는지 확인하십시오. – MrSmith42
'catch (IOException e) { //e.printStackTrace(); }'. 그 stacktrace 인쇄하자! 그리고 e.getMessage()를 반환하고 onPostExecute에서 검사합니다. 이제 아무 것도 모릅니다! – greenapps
'onPostExecute (무효 결과)'. 그 순간 doInBoackground는 항상'null'를 반환하므로'result'는 항상'null'입니다. – greenapps