Cant upload image to server using Asynchttpclient in android

北城余情 提交于 2019-12-13 18:27:47

问题


I am using AsyncHttpClient for uploading image and data to server.But data and image are not uploaded. I dont know what mistake i have done. Can anyone help me?

My Code:

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);
                }

            }
        });
    }
}

My Server Code:

$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();
?>

while posting only string values, i can able to upload data to server. But when i upload image, its not uploaded.


回答1:


replace

params.put("img",myFile);

with

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



回答2:


The mistake what i done here is, i am sending image file using 'img' keyword. But in php file, i have used different keyword to accept. Thats the problem, now i changed keyword to img(as same in android code). now its working fine.

  $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.";

Thanks to all who helped me.



来源:https://stackoverflow.com/questions/27149315/cant-upload-image-to-server-using-asynchttpclient-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!