【发布时间】:2015-03-19 02:48:41
【问题描述】:
我正在尝试通过 REST API 上传图像。当我尝试使用 HttpURL 连接时,它会返回 500 个响应代码。我还尝试通过多种方式解决问题,但不要找出问题。我的代码
URL url = new URL(urlLink);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","multipart/form-data;boundary=----------V2ymHFg03ehbqgZCaKO6jy");
connection.setRequestProperty("DocumentId", ""+ AppConstant.DocumentId);
connection.setRequestProperty("Host", "www.cashsheet.com");
connection.setRequestProperty("Authorization", au);
connection.setRequestProperty("UserId", "" + AppConstant.UserId);
int responseCode = connection.getResponseCode();
Log.w("responseCode", "are"+responseCode);
try {
InputStream inputStream = connection.getInputStream();
} catch (IOException ioe) {
connection.disconnect();
Log.w("IOException", "are"+ioe.getMessage());
// If HTTP response code '401' is returned then try to resume
// the expired
// session on the reporting server by obtaining a new HTTP URL
// Connection.
if (connection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
//throw new ClusterException(
// "The access to the requested resource was denied; the request may have been made without authorization credentials.");
}
// If response code other than '401' is returned then propagate
// the IOException to the next level.
else {
//throw new ClusterException(ioe.getMessage());
}
}
它总是重新运行响应代码 500 并在 urlLink 上显示错误; urlLink ="http://www.cashsheet.com/api/upload" 。
我无法捕捉到我的错误:(。请帮助我如何解决这个问题。谢谢大家。
【问题讨论】:
标签: java android rest upload image-uploading