我正在开发一个 Android 应用程序,用户可以在其中上传您访问的场所的照片。我正在使用端点并已开始通过 Apigee 控制台测试https://api.foursquare.com/v2/photos/PHOTO_ID 。我遇到的问题是,如果我通过控制台发送图像,则会返回错误 400 与 mime 类型有关的问题,因为您可以在此静止图像中正确,我认为。 https://docs.google.com/file/d/0B9uUMZ3ZVbl_bG5rOUNhM01wb2M/edit?usp=sharing(控制台错误)我也尝试通过我正在开发的应用程序上传,但我返回了同样的错误。app中的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 的数据上。有人有这方面的经验吗?谢谢