无法使用Android中的Asynchttpclient将图像上传到服务器

时间:2014-11-26 12:30:52

标签: 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.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();
?>

在仅发布字符串值时,我可以将数据上传到服务器。但是当我上传图片时,它没有上传。

2 个答案:

答案 0 :(得分:2)

替换

params.put("img",myFile);

 params.put("img",new FileInputStream(myFile));

答案 1 :(得分:1)

我在这里做的错误是,我正在使用'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.";

感谢所有帮助过我的人。