I created the UploadToImgurTask class as AsyncTask, which takes one parameter of the file path, creates and installs MultiPartEntity, and then uses the Apache HttpClient to load the image with the specified object. Imgur's JSON response is stored in a JSONObject, the contents of which I display in LogCat for my own understanding.
Here is a screenshot of the JSON I get from Imgur:

I was looking for Status 401 error on api.imgur.com and it says that I need to authenticate using OAuth despite the fact that Imgur very clearly stated that applications do not need to use OAuth if the images are downloaded anonymously (this is what I'm doing right now).
class UploadToImgurTask extends AsyncTask<String, Void, Boolean> { String upload_to; @Override protected Boolean doInBackground(String... params) { final String upload_to = "https://api.imgur.com/3/upload.json"; final String API_key = "API_KEY"; final String TAG = "Awais"; HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(upload_to); try { final MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("image", new FileBody(new File(params[0]))); entity.addPart("key", new StringBody(API_key)); httpPost.setEntity(entity); final HttpResponse response = httpClient.execute(httpPost, localContext); final String response_string = EntityUtils.toString(response .getEntity()); final JSONObject json = new JSONObject(response_string); Log.d("JSON", json.toString());
After doInBackground returns a link to the downloaded image on onPostExecute, I want to copy it to the system clipboard, but Eclipse continues to say that getSystemService (String) is not defined in my ASyncTask class.
There is no legal way to return a (String) link back to the main thread, so I have to do everything I have to do in onPostExecute, in UploadToImgurTask (which extends ASyncTask)
@Override protected void onPostExecute(String result) { super.onPostExecute(result); ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("label", "Text to copy"); clipboard.setPrimaryClip(clip); }
What causes the problem?
source share