我对安卓很陌生,第一次这样做。我正在尝试在我的应用程序中下载 pdf 表单 url,但它没有被下载。我真的搞砸了。我不知道我错过了什么,为什么这对我不起作用。请帮我做到这一点。
在这里,我粘贴了我的代码:
public class ProductBrochureActivity extends Activity {
private static String cookie;
private static String nid;
WebView webViewForBrochureAndVideo;
private String prodBrochureURL;
private String prodVideoURL;
private static int clickedItemId;
ActionBar actionBar;
private static HashMap<String, String> cookieWithRequest = new HashMap<String, String>();
static Object json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_brochure);
actionBar = getActionBar();
actionBar.hide();
Intent intent = getIntent();
cookie = intent.getStringExtra(BsharpConstant.WEB_SERVICES_COOKIES);
nid = intent.getStringExtra(BsharpConstant.PRODUCT_NODE_ID);
clickedItemId = intent.getIntExtra(BsharpConstant.CLICKED_ITEM_ID, 0);
String jsonResponseFromWebservices = WebserviceBsharpUtil
.callWebServicesToGetTheProductBrochureAndVideo(cookie, nid);
urlFromResponse(jsonResponseFromWebservices);
cookieWithRequest.put(BsharpConstant.WEB_SERVICES_COOKIES, cookie);
switch (clickedItemId) {
case 0:
if (!prodBrochureURL.isEmpty()) {
try {
new DownloadFile();
} catch (ActivityNotFoundException e) {
Toast.makeText(this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "No PDF is Attached with this Product",
Toast.LENGTH_SHORT).show();
}
break;
case 1:
if (!prodVideoURL.isEmpty()) {
try {
new DownloadFile();
} catch (ActivityNotFoundException e) {
Toast.makeText(this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
break;
} else {
Toast.makeText(this, "No Video is Attached with this Product",
Toast.LENGTH_SHORT).show();
}
}
}
/**
* GetTheBrochureAndAttachedVideoURL
*
* @param jsonResponse
*/
public void urlFromResponse(String jsonResponse) {
try {
json = new JSONTokener(jsonResponse).nextValue();
if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
prodBrochureURL = jsonArray.getJSONObject(0).getString(
BsharpConstant.PRODUCT_BROCHURE_URL);
prodVideoURL = jsonArray.getJSONObject(0).getString(
BsharpConstant.PRODUCT_VIDEO_URL);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private class DownloadFile extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String filename = "brochure.pdf";
HttpURLConnection connection;
try {
URL url = new URL(prodBrochureURL);
connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty(
BsharpConstant.WEB_SERVICES_COOKIES, cookie);
connection.setDoOutput(true);
connection.connect();
} catch (IOException e1) {
return e1.getMessage();
}
File folderDir = new File(getExternalFilesDir("Bsharp_PDF")
+ "/Download");
File file = new File(folderDir, filename);
if (file.exists()) {
file.delete();
}
if ((folderDir.mkdirs() || folderDir.isDirectory())) {
try {
InputStream inputStream = connection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(
folderDir + "/" + filename);
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len1);
}
fileOutputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(),
"Unable to create folder", Toast.LENGTH_LONG).show();
}
return "Done";
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG)
.show();
super.onPostExecute(result);
}
}
}
为了执行AsyncTask(在你的例子中是DownloadFile),必须显式调用它的execute(Params...参数)方法。在您的情况下,除了实例化您的任务调用之外,执行而不提供任何参数,即
DownloadFile task = new DownloadFile();
task.execute();
希望这有帮助。