无法在android中使用Asynchttpclient将图像上载到服务器

无法在android中使用Asynchttpclient将图像上载到服务器,android,asynchttpclient,Android,Asynchttpclient,我正在使用AsyncHttpClient将图像和数据上载到服务器。但数据和图像未上载。我不知道我犯了什么错误。有人能帮我吗 我的代码: package com.example.asynchttpclientex; import java.io.File; import java.io.FileNotFoundException; import org.apache.http.Header; import android.os.Bundle; import android.support.v

我正在使用AsyncHttpClient将图像和数据上载到服务器。但数据和图像未上载。我不知道我犯了什么错误。有人能帮我吗

我的代码:

package com.example.asynchttpclientex;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.http.Header;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class MainActivity extends ActionBarActivity {

    Button a;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        a=(Button)findViewById(R.id.b);

        a.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                                       String imagePath="file://sdcard/Images/tempge.jpg";
                AsyncHttpClient client = new AsyncHttpClient();
                File myFile = new File(imagePath);
                RequestParams params = new RequestParams();
                try {
                    params.put("img", myFile);
                    params.put("action", "hello data");

                    client.post("http://xxxxx.com/android/adem.php", params, new AsyncHttpResponseHandler() {

                        @Override
                        public void onStart() {
                        super.onStart();
                        }

                        @Override
                        public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                                Throwable arg3) {
                            // TODO Auto-generated method stub
                            Toast.makeText(getApplicationContext(), "failure...!", Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                            // TODO Auto-generated method stub
                            Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
                        }

                    });
                } catch(FileNotFoundException e) {
                    Log.d("MyApp", "File not found!!!" + imagePath);
                }

            }
        });
    }
}
我的服务器代码:

$target_path1 = "upload_image/";
$randno=mt_rand(1,15000);

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */


$target_path2 = $target_path1 . basename($randno.$_FILES['img']['name']);
if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path2)) {
    echo "The first file ".  basename($randno. $_FILES['uploadedfile1']['name']).
    " has been uploaded.";

$thumbimage1="http://www.xxxxx.com/android/".$target_path2;


} else{
    echo "There was an error uploading the file, please try again!";
    echo "filename: " .  basename( $_FILES['uploadedfile1']['name']);
    echo "target_path: " .$target_path2;
}

$proj_name = $_POST['action'];

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  mysql_select_db("xxxxx_nikapp");
$query = mysql_query("insert into sfc(name, contact_no, lr_no, details) values ('$proj_name','$proj_name','$proj_name','$thumbimage1')");


  mysql_close();
?>
虽然只发布字符串值,但我可以将数据上载到服务器。但是当我上传图像时,它没有被上传。

替换

params.put("img",myFile);


我在这里所做的错误是,我使用'img'关键字发送图像文件。但在php文件中,我使用了不同的关键字来接受。这就是问题所在,现在我把关键字改成了img(和android代码一样)。现在它可以正常工作了

  $target_path2 = $target_path1 . basename($randno.$_FILES['img']['name']);
if(move_uploaded_file($_FILES['img']['tmp_name'], $target_path2)) {
    echo "The first file ".  basename($randno. $_FILES['img']['name']).
    " has been uploaded.";

感谢所有帮助我的人。

您需要将字节[]发送到服务器,而不是本地文件的路径file@LenaBru如何发布字节[]?@LenaBru正在将图像上载到特定文件夹,并将图像路径上载到数据库。是吗?当你尝试读取$_FILES数组时,它应该包含字节,而不是字符串。你的android代码发送的是字符串而不是文件。使用此选项后,我无法上传图像
  $target_path2 = $target_path1 . basename($randno.$_FILES['img']['name']);
if(move_uploaded_file($_FILES['img']['tmp_name'], $target_path2)) {
    echo "The first file ".  basename($randno. $_FILES['img']['name']).
    " has been uploaded.";