Android-async-http upload image to php server

I watched all day how to upload an image from my application to the server. I made the mobile app side

public static void postImage(String ImageLink){ RequestParams params = new RequestParams(); params.put("uploaded_file[name]", "MyImageName"); try { params.put("uploaded_file[image]", new File(ImageLink)); } catch (FileNotFoundException e) { e.printStackTrace(); } AsyncHttpClient client = new AsyncHttpClient(); client.post(MY_PHP_FILE_LINK, params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { System.out.println("statusCode "+statusCode);//statusCode 200 } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { } }); } 

and my php side code

 <?php $file_path = "/uploads/"; $file_path = $file_path . basename( $_FILES['uploaded_file']['name']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) { echo "success"; } else{ echo "fail"; }?> 

but I can’t find the image on the server, even the status code was OK

What is the problem? wrong php code?

+4
source share
1 answer

I solved the problem, it was in my RequestParams, I tried to change the file name, but it seems I can not do this to change my code, and it works fine

 public static void postImage(String ImageLink){ RequestParams params = new RequestParams(); try { params.put("uploaded_file", new File(ImageLink)); } catch (FileNotFoundException e) { e.printStackTrace(); } AsyncHttpClient client = new AsyncHttpClient(); client.post(MY_PHP_FILE_LINK, params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { System.out.println("statusCode "+statusCode);//statusCode 200 } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { } }); } 
+9
source

All Articles