我正在尝试编写代码,上传从我的Google Glass中拍摄的照片以获取并获取网址。奇怪的是,我收到了imgur URL的FileNotFoundException。我能做错什么?这是我从其他地方借来的代码的混杂,但似乎无法让它正常工作......我也尝试过使用网址https://api.imgur.com/3/image
图像文件当前位于/storage/emulated/0/DCIM/Camera/20141123_120731_953.jpg
public class UploadImgur extends AsyncTask<String, Integer, String> {
private static final String imgurURL = "https://api.imgur.com/3/";
private static final String clientID = "*****"; // filled in in my code
private static final String clientSecret = "*****"; // filled in in my code
private static final String TAG = "UploadImgur";
@Override
protected String doInBackground(String... sourceFileURL) {
Log.i(TAG, "Now establishing a connection online...");
String fileName = sourceFileURL[0];
try {
URL url;
url = new URL(imgurURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String data = URLEncoder.encode("image", "UTF-8") + "="
+ URLEncoder.encode(fileName, "UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Client-ID " + clientID);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
Log.i(TAG, "Connection Open");
conn.connect();
StringBuilder stb = new StringBuilder();
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
Log.i(TAG, "Data written");
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
stb.append(line).append("\n");
}
Log.i(TAG, "Response Received");
wr.close();
rd.close();
resultURL = stb.toString();
} catch(MalformedURLException me) {
Log.e(TAG, "Malformed URL " + imgurURL);
} catch(UnsupportedEncodingException uee) {
Log.e(TAG, "UnsupportedEncoding Exception: UTF-8");
} catch(IOException ioe){
ioe.printStackTrace();
Log.e(TAG, ioe.toString());
}
return resultURL;
}
protected void onPostExecute(String result) {
Log.i(TAG,result);
Log.i(TAG,resultURL);
}
}
错误:
11-23 12:07:40.465 20090-20342/com.example.cerveau.recognizeplaces I/UploadImgur﹕ Connection Open
11-23 12:07:40.700 20090-20342/com.example.cerveau.recognizeplaces D/dalvikvm﹕ GC_FOR_ALLOC freed 334K, 7% free 5272K/5644K, paused 29ms, total 29ms
11-23 12:07:40.762 20090-20342/com.example.cerveau.recognizeplaces I/UploadImgur﹕ Data written
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ java.io.FileNotFoundException: https://api.imgur.com/3/
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at com.example.cerveau.recognizeplaces.UploadImgur.doInBackground(UploadImgur.java:61)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at com.example.cerveau.recognizeplaces.UploadImgur.doInBackground(UploadImgur.java:19)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:302)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:240)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
11-23 12:07:40.801 20090-20342/com.example.cerveau.recognizeplaces E/UploadImgur﹕ java.io.FileNotFoundException: https://api.imgur.com/3/