Android 图像共享到 ImageShack 站点

Posted

技术标签:

【中文标题】Android 图像共享到 ImageShack 站点【英文标题】:Android Image Sharing to ImageShack Site 【发布时间】:2012-12-07 14:27:44 【问题描述】:

我正在开发一个安卓应用。在这个应用程序中,需要使用他们的 api 将图像上传到 imageShack 网站。

这里的“sourceFileUri”是来自设备sdcard的图像文件路径。它显示Outh或dev Key无效。。请任何人都可以帮助我找出错误。高级感谢。

private void goForUpload(final String sourceFileUri)

    if (!SharedPreferencesHelper.isOnline(con))
    
        return;
    

    pDialog = ProgressDialog.show(this, "Please wait...", "Loading...",false, false);

    final Thread d = new Thread(new Runnable() 
    
        @Override
        public void run() 

            String upLoadServerUri = " http://www.imageshack.us/upload_api.php";

            String fileName = sourceFileUri;

            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            File sourceFile = new File(sourceFileUri);

            Log.w("file name are...", "" + sourceFile);
            if (!sourceFile.isFile()) 
                Log.e("uploadFile", "Source File Does not exist");
            

            try  // open a URL connection to the Servlet
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL(upLoadServerUri);
                conn = (HttpURLConnection) url.openConnection(); // Open a
                                                                    // HTTP
                                                                    // connection
                                                                    // to
                                                                    // the
                                                                    // URL
                conn.setDoInput(true); // Allow Inputs
                conn.setDoOutput(true); // Allow Outputs
                conn.setUseCaches(false); // Don't use a Cached Copy
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                //conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
                conn.setRequestProperty("uploaded_file", fileName);

                dos = new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                // ///////////////////////////////////////////////////////////////////

                //for image
                dos.writeBytes("Content-Disposition: form-data; name=\"fileupload\";filename=\""+ fileName + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);

                dos.writeBytes("Content-Disposition: form-data; name=\"key\""+ lineEnd);
                dos.writeBytes(lineEnd);
                dos.writeBytes("289DGHSTbbfb01094c0017d23e96fe1edecda161");
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + lineEnd);


                bytesAvailable = fileInputStream.available(); 

                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                // read file and write it into form...
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) 
                
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                

                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                // Responses from the server (code and message)
                serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();
                InputStream servere = conn.getInputStream();
                String uyy = HttpRequest.GetText(servere);

                System.out.print(uyy);

                Log.w("uyy", "" + uyy);

                Log.i("uploadFile", "HTTP Response is : "+ serverResponseMessage + ": " + serverResponseCode);

                if (serverResponseCode == 200) 
                
                    runOnUiThread(new Runnable()
                    
                        public void run()
                        
                            Toast.makeText(MainActivity.this,"File Upload Complete.",Toast.LENGTH_SHORT).show();
                        
                    );
                

                // close the streams //
                fileInputStream.close();
                dos.flush();
                dos.close();

                // Log.w("url", ""+url);

                // final String url =
                // "http://www.imageshack.us/upload_api.php?fileupload=http://www.libpng.org/pub/png/img_png/pnglogo-blk.jpg&url="+fos+"&optsize=100x100&rembar=yes&key=DHKNPRWYb185051e16c4545d8f26828d6fd3886c";

                // final String url =
                // "https://api.mobypicture.com/2.0/upload.json";//key=Az7IN9Qaxu3eZeK5/media=http://www.libpng.org/pub/png/img_png/pnglogo-blk.jpg/message=wasir";
                //
                // final String result = HttpRequest.GetText(HttpRequest
                // .getInputStreamForGetRequest(url));
                //
                // try 
                // if (parser.connect(con, result)) 
                // Log.d("What is result :", result);
                // 
             catch (Exception e) 
                // TODO Auto-generated catch block
                e.printStackTrace();
            

            /*
             * update the GUI
             */

            runOnUiThread(new Runnable() 

                @Override
                public void run() 

                    if (pDialog != null) 
                        pDialog.cancel();
                    
                

            );

        
    );

    d.start();


【问题讨论】:

dev Key 无效 -> 你确定 ImageShack 的 dev key 有效吗? 是的,因为我可以通过浏览器使用此 api 密钥上传图像。 12-08 11:31:14.417: W/Respone(9988): 12-08 11:31:14.417: W/Respone(9988):** 您必须提供有效的身份验证令牌或开发密钥。见code.google.com/p/imageshackapi/</error>** 12-08 11:31:14.417: W/Respone(9988): 这是logcat中显示的错误 你在哪里能弄明白?我尝试在下面实现您的代码和解决方案。我不得不注释掉所有 HttpRequest 的东西,因为它是无效的。所以我无法阅读我的回复文本。但是我能够提交并收到响应代码 200,但是图片从未出现在视线中。非常令人沮丧。任何提示将不胜感激 【参考方案1】:

第一个问题是您拥有表单数据部分的顺序:

            dos.writeBytes("Content-Disposition: form-data; name=\"key\""+ lineEnd);
            dos.writeBytes(lineEnd);
            dos.writeBytes("xxxxxxxxxxxxxxxxxx");
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + lineEnd);

            //for image
            dos.writeBytes("Content-Disposition: form-data; name=\"fileupload\";filename=\""+ fileName + "\"" + lineEnd);
                dos.writeBytes("Content-Type: image/jpeg" + lineEnd);
                dos.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
            dos.writeBytes(lineEnd);
            //dos.writeBytes(twoHyphens + boundary + lineEnd);

第二个问题(当我尝试时)是图像类型无效,所以我添加了如上所示的内容类型。在写入文件内容之前,您不需要边界。

【讨论】:

感谢查尔斯·沃尔夫。

以上是关于Android 图像共享到 ImageShack 站点的主要内容,如果未能解决你的问题,请参考以下文章

到 Imageshack 的 HTTP POST

使用android意图将带有图像的文本共享到instagram

Imageshack API几乎可以工作

Imageshack 上传和 wrong_file_type

将 github imageshack SDK 与 Android Studio 集成

解析从 imageshack API 返回的数据