[英]Image Upload to php server android
我正在嘗試將圖像上傳到服務器端目錄。這是我的代碼。當我單擊提交按鈕時,它不是上傳圖像。LogCat中沒有任何錯誤。
mBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String selectedPath = "file://storage/emulated/0/Pictures/Usage Tracker/IMG_20150916_161450.jpg";
try{
File file=new File(selectedPath);
RequestParams params = new RequestParams();
params.put("uploadedfile",file);
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://example.com/utracker/webservices/upload_picture.php", params, new AsyncHttpResponseHandler() {
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String s=new String(responseBody);
if(s.contentEquals("Success")){
Toast.makeText(MainActivity.this,
"You Have Successfully Uploaded Your Image To Our Server",
Toast.LENGTH_LONG).show();
}
}
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
catch (Exception e)
{
}
}
}
PHP腳本
<?php
$target_path = "../uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
//echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
echo "Success";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
您無法通過在echo中打印來獲取圖像上傳的成功或失敗,您必須將json編碼的響應返回給android,然后檢查它是成功還是失敗
if(mysql_num_rows($res) > 0) {
$response["success"] = 1;
$response["message"] = "Success";
echo json_encode($response);
}
else{
$response["success"] = 0;
$response["message"] = "Failure";
echo json_encode($response);
}
請參閱此鏈接http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.