Android图像共享到ImageShack站点

Android图像共享到ImageShack站点,android,imageshack,Android,Imageshack,我正在开发一个android应用程序。在此应用程序中,需要使用其api将图像上载到imageShack站点 这里的“sourceFileUri”是来自设备SD卡的映像文件路径。它显示Outh或dev密钥无效。。请任何人帮我找出错误。非常感谢 private void goForUpload(final String sourceFileUri) { if (!SharedPreferencesHelper.isOnline(con)) { return;

我正在开发一个android应用程序。在此应用程序中,需要使用其api将图像上载到imageShack站点

这里的“sourceFileUri”是来自设备SD卡的映像文件路径。它显示Outh或dev密钥无效。。请任何人帮我找出错误。非常感谢

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();

}

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

            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);

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

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

            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);

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

开发密钥无效->您确定ImageShack的开发密钥有效吗?是的,因为我可以通过浏览器使用此api密钥上载图像。12-08 11:31:14.417:W/Respone(9988):12-08 11:31:14.417:W/Respone(9988):**您必须提供有效的身份验证令牌或开发密钥。请参见code.google.com/p/imageshackapi/**12-08 11:31:14.417:W/Respone(9988):这是日志中显示的错误,您可以在哪里找到答案?我尝试实现了下面的代码和解决方案。我不得不注释掉所有的HttpRequest内容,因为它是无效的。所以我无法阅读我的回复文本。但我能够提交并收到回复代码200,然而这些照片从未出现。非常令人沮丧。任何提示都会被欣赏dev Key无效->您确定ImageShack的dev Key有效吗?是的,因为我可以通过浏览器使用此api Key上载图像。12-08 11:31:14.417:W/Respone(9988):12-08 11:31:14.417:W/Respone(9988):**您必须提供有效的身份验证令牌或dev Key。请参见code.google.com/p/imageshackapi/**12-08 11:31:14.417:W/Respone(9988):这是日志中显示的错误,您可以在哪里找到答案?我尝试实现了下面的代码和解决方案。我不得不注释掉所有的HttpRequest内容,因为它是无效的。所以我无法阅读我的回复文本。但我能够提交并收到回复代码200,然而这些照片从未出现。非常令人沮丧。谢谢查尔斯·沃尔夫,谢谢查尔斯·沃尔夫。