2014-01-06 66 views
0

我是非常新的android和第一次這樣做。我正嘗試在我的應用程序中下載pdf表單url,但沒有下載。我真的被這個搞砸了。我不知道我錯過了什麼,爲什麼這不適合我。請幫助我做到這一點。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); 
    } 
} 

}

+0

如果您有任何錯誤,您可以發佈日誌嗎?你有沒有在你的Manifest中添加Internet權限,即

+0

@kanak索尼我已經添加了這個,並且還添加了但仍然無法正常工作。我沒有得到任何錯誤,它唯一沒有得到下載 – user3154663

回答

1

爲了一個的AsyncTask(DownloadFile你的情況)執行的一個具有顯式調用它的執行(參數... PARAMS ) 方法。在除了實例化你的任務調用的情況下執行沒有提供任何參數,即

DownloadFile task = new DownloadFile(); 
task.execute(); 

希望這有助於。

+0

你是我的知識.... haha​​hahaha – user3154663