在Android应用中错误上传图片 - Api Foursquare

时间:2013-10-14 09:38:13

标签: java android foursquare image-upload

我正在开发一个Android应用程序,用户可以在其中上传您访问的场所的照片。我正在使用端点,并已开始通过Apigee控制台测试https://api.foursquare.com/v2/photos/PHOTO_ID。我遇到的问题是,如果我通过控制台发送图像,则返回错误400 mime类型的问题,因为你可以在这个静止图像中是正确的,我想。 https://docs.google.com/file/d/0B9uUMZ3ZVbl_bG5rOUNhM01wb2M/edit?usp=sharing(控制台错误) 我也尝试通过我正在开发的应用程序上传,并返回相同的错误。应用程序中的execute方法如下:

        TokenStore tokenStore=new TokenStore(getApplicationContext());
        String token=tokenStore.getSavedToken();

        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;

        String pathToOurFile = params[0];

        String urlServer = "https://api.foursquare.com/v2/photos/add?public=1&venueId=5252da418bbd79f3aaa70ae6&oauth_token="+token;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary =  "*****";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;

        try  {
            FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "image/jpeg");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

            outputStream = new DataOutputStream( connection.getOutputStream() );
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
            //outputStream.writeBytes("Content-Type: image/jpeg");
            outputStream.writeBytes(lineEnd);

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

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

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

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            Log.i("serverResponse", "serverresponse "+ serverResponseMessage);
            Log.i("serverResponseCode", "serverResponseCode "+ serverResponseCode);


            fileInputStream.close();
            outputStream.flush();
            outputStream.close();



        }
        catch (Exception ex) {
        //Exception handling
        }

在我的测试在线服务器中,上传图像类工作正常,因此问题必须是某些类型的数据不适合Foursquare API。有人有这方面的经验吗?感谢

0 个答案:

没有答案