我是android的新手,我尝试将图像文件直接发送到我的服务器上。我不知道该怎么做。我正在使用post方法在我的服务器中使用JAX-RS Restful Web服务。
这是我的代码(Android代码):
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
params.put("image", myFile);
UpdateProfilePicToServer(params);
} catch(FileNotFoundException e) {}
private void UpdateProfilePicToServer(RequestParams params) {
// Show Progress Dialog
prgDialog.show();
// Make RESTful webservice call using AsyncHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
// client.setTimeout(20000);
client.post(URL, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
// Hide Progress Dialog
prgDialog.dismiss();
try {
// JSON Object
JSONObject obj = new JSONObject(new String(responseBody));
// When the JSON response has status boolean value assigned with true
if (obj.getBoolean("status")) {
Toast.makeText(context.getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
}
// Else display error message
else {
profilePic.setImageResource(R.drawable.profile_placeholder);
Toast.makeText(context, obj.getString("msg"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(context, "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// Hide Progress Dialog
prgDialog.dismiss();
// When Http response code is '404'
if (statusCode == 404) {
Toast.makeText(context, "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if (statusCode == 500) {
Toast.makeText(context, "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else {
Log.d("StatusCode", String.valueOf(statusCode));
Toast.makeText(context, "Check your internet connection", Toast.LENGTH_LONG).show();
}
}
});
}
这是我的网络服务代码(Java):
public String post(@FormParam("accId") String accId, @FormParam("image") File image) throws Exception
{
try {
Statement chkUserStmt = DataAccess.conn.createStatement();
String updatequery = "UPDATE ACCOUNT SET PROFILE_PICTURE = LOAD_FILE('" + image+ "') "
+ "WHERE ACCOUNT_ID = '" + accId + "'";
chkUserStmt.executeUpdate(updatequery);
chkUserStmt.close();
return Utility.respJSON("ImageStore ", true, "Success");
} catch (Exception ex) {
Logger.getLogger(ImageStore.class.getName()).log(Level.SEVERE, null, ex);
}
return Utility.respJSON("ImageStore ", false, "Failure");
}
我的Logcat:
10-26 16:04:38.347 8198-9571/com.h2o W/System.err: org.apache.http.client.ClientProtocolException
10-26 16:04:38.352 8198-9571/com.h2o W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:582)
10-26 16:04:38.352 8198-9571/com.h2o W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:503)
10-26 16:04:38.352 8198-9571/com.h2o W/System.err: at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:148)
10-26 16:04:38.352 8198-9571/com.h2o W/System.err: at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:179)
10-26 16:04:38.352 8198-9571/com.h2o W/System.err: at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:108)
10-26 16:04:38.352 8198-9571/com.h2o W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
10-26 16:04:38.353 8198-9571/com.h2o W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-26 16:04:38.353 8198-9571/com.h2o W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-26 16:04:38.353 8198-9571/com.h2o W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-26 16:04:38.353 8198-9571/com.h2o W/System.err: at java.lang.Thread.run(Thread.java:818)
10-26 16:04:38.353 8198-9571/com.h2o W/System.err: Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
10-26 16:04:38.353 8198-9571/com.h2o W/System.err: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:439)
10-26 16:04:38.353 8198-9571/com.h2o W/System.err: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:580)
我不知道我做错了什么..请任何人帮助我
提前致谢。