从Dropbox(V2)中挑选文件时,我正在尝试使用以下代码获取共享链接网址。
internal inner class GetDropboxSharableURLTask(file_path: String, pathLower: String) : AsyncTask<String, Void, Void>() {
private var pDialog: ProgressDialog? = null
private var dropbox_url: String? = file_path
private var db_pathLower: String? = pathLower
override fun onPreExecute() {
super.onPreExecute()
pDialog = ProgressDialog(context)
pDialog!!.setCancelable(false)
pDialog!!.setMessage(context?.resources?.getString(R.string.please_wait))
pDialog!!.show()
}
override fun doInBackground(vararg urls: String): Void? {
try {
val shareLink = DropboxClientFactory.getClient().sharing().getSharedLinkMetadata(db_pathLower)
dropbox_url = getShareURL(shareLink.url)!!.replaceFirst("https://www".toRegex(), "https://dl")
return null
} catch (e: Exception) {
e.printStackTrace()
return null
}
}
fun getShareURL(strURL: String?): String? {
var conn: URLConnection? = null
var redirectedUrl: String? = null
try {
val inputURL = URL(strURL)
conn = inputURL.openConnection()
conn!!.connect()
val `is` = conn.getInputStream()
println("Redirected URL: " + conn.url)
redirectedUrl = conn.url.toString()
`is`.close()
} catch (e: MalformedURLException) {
Log.d("", "Please input a valid URL")
} catch (ioe: IOException) {
Log.d("", "Can not connect to the URL")
}
return redirectedUrl
}
override fun onPostExecute(feed: Void?) {
// TODO: check this.exception
// TODO: do something with the feed
pDialog!!.hide()
Log.d("url", dropbox_url)
val intent = Intent()
intent.putExtra(Constant.KEY_DROPBOX_PICKER, dropbox_url)
context?.setResult(Activity.RESULT_OK, intent)
context?.finish()
}
}
我收到如下错误 -
com.dropbox.core.v2.sharing.SharedLinkErrorException:Exception in 2 / sharing / get_shared_link_metadata:SHARED_LINK_NOT_FOUND
调用getSharedLinkMetadata(db_pathLower)
虽然早些时候,我在dropbox V1
使用了类似的代码,但是当我切换到新版本的dropbox API时,即dropbox V2
,我在尝试时遇到此错误获取dropbox的实际共享URL,(也必须包含文件扩展名的那个)。
预期的网址应该是这样的 - https://dl.dropbox.com/s/hpe1gjfb4aqsv3e/property_1523965639_img.jpeg?dl=0
但是,我得到的只是https://dl.dropboxusercontent.com/apitl/1/AAD62_lYN5n_cYyRQWhg_rmXGJPFzqF5m8OJpNt_SIIxG7bVvmO6X5d1pKg7uulM1vEBWx_X9PZ9i3vFy-jb3eBC-M_q3YCWRmPrdAwpQ7kqSFGCIPrZaHNC44YRjwXGXYTbnqMO1hPhKb-G5matDzTABUQOssB-LIN4qWoJmPnuhNgzpL9FO4ibet4uBPoef_SLZLjupsOV9PKYUhtPxY_NY7HjymZSHsQh67m4HoBN4YgEAPot0KMAsV1eE3WCjK0XbD1YfGqdsVI9H40KUQ_9R-nmAouoqdbA37G5CXjQKYPC8cENTvN2pjHKwCnHvgI
,因为那个错误。
请让我知道我需要修改的内容,以便获取dropbox共享URL(以及文件扩展名),并在选择/单击文件后立即避免上述错误。< / p>
答案 0 :(得分:0)
最后,在this的帮助下解决了,因为listSharedLinksBuilder
终于为我工作了。
通过将代码修改为 -
解决了问题 val sharedLinksResults = DropboxClientFactory.getClient().sharing().listSharedLinksBuilder().withPath(db_pathLower).withDirectOnly(true).start()
var url = ""
if (sharedLinksResults.links.isEmpty()) {
val shareLink = DropboxClientFactory.getClient().sharing().createSharedLinkWithSettings(db_pathLower) //.getClient().sharing().getSharedLinkMetadata(db_pathLower)
url = shareLink.url
} else {
url = sharedLinksResults.links[0].url
}
dropbox_url = getShareURL(url)!!.replaceFirst("https://www".toRegex(), "https://dl")