1

I'm trying to implement sample form with multiple images submit to the php server.Below are the code snippets i used to implement

When submit

 b3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!(selectedPath1.trim().equalsIgnoreCase("NONE")) && !(selectedPath2.trim().equalsIgnoreCase("NONE"))){
                    progressDialog = ProgressDialog.show(MainActivity.this, "", "Uploading files to server.....", false);
                    Thread thread=new Thread(new Runnable(){
                        public void run(){
                            doFileUpload();
                            runOnUiThread(new Runnable(){
                                public void run() {
                                    if(progressDialog.isShowing())
                                        progressDialog.dismiss();
                                }
                            });
                        }
                    });
                    thread.start();
                }else{
                    Toast.makeText(getApplicationContext(),"Please select two files to upload.", Toast.LENGTH_SHORT).show();
                }
            }
        });

doFileUpload()

private void doFileUpload(){
       File file1 = new File(selectedPath1);
        File file2 = new File(selectedPath2);
        String urlString = "http://url/api";
        try
        {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(urlString);
            FileBody bin1 = new FileBody(file1);
            FileBody bin2 = new FileBody(file2);
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("image1", bin1);
            reqEntity.addPart("image2", bin2);
            reqEntity.addPart("section", new StringBody("inventory"));
            reqEntity.addPart("action", new StringBody("new"));
            reqEntity.addPart("apron_id", new StringBody("465464f313164d6464fds64f6d4"));
            reqEntity.addPart("nickname", new StringBody("test"));
            reqEntity.addPart("location", new StringBody(2+""));
            reqEntity.addPart("manufacture", new StringBody(3+""));
            reqEntity.addPart("core_material", new StringBody("test"));
            reqEntity.addPart("color", new StringBody("test"));
            reqEntity.addPart("Date_purchase", new StringBody("25/10/1991"));
            reqEntity.addPart("UID_no", new StringBody("546345643465434554436554"));
            reqEntity.addPart("serial", new StringBody("46544624463464423644634244545"));
            reqEntity.addPart("Batch", new StringBody("464546631313464"));
            reqEntity.addPart("Expiration", new StringBody("25/10/2091"));
            reqEntity.addPart("garment_type", new StringBody("test"));
            reqEntity.addPart("QTY", new StringBody("4"));
            reqEntity.addPart("user_id", new StringBody(2+""));

            post.setEntity(reqEntity);
            HttpResponse response = client.execute(post);
            resEntity = response.getEntity();
            final String response_str = EntityUtils.toString(resEntity);
            if (resEntity != null) {
                Log.i("RESPONSE",response_str);
                runOnUiThread(new Runnable(){
                    public void run() {
                        try {
                            res.setTextColor(Color.GREEN);
                            res.setText("n Response from server : n " + response_str);
                            Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
        catch (Exception ex){
            Log.e("Debug", "error: " + ex.getMessage(), ex);
        }
    }

App seems to be sent post data but no data received at server below is the php script

<?php
require_once("../includes/DbConfig.php");
require_once("../includes/classes/general.php");
$status  =1;
$response=array();
$data=$_REQUEST;
$action=$data['action'];
$section=$_REQUEST['section'];

file_put_contents("out.txt","\n".implode("\n",$_REQUEST)." \n ".implode("\n",$_FILES)." \n date ".date("Y-d-m H:i:s"),FILE_APPEND);

              if($section=="inventory")
                    {
                        if(empty($action))
                        {
                            $response=array("status"=>0,"data"=>"No action Found");
                            echo json_encode($response);
                            exit;
                        }
                        require_once("classes/class_inventory.php");
                        $inventory = new inventory();
                        switch($action)
                        {
                            case 'new':
                                    $response=$inventory->action_inventory($_REQUEST);
                                    echo json_encode($response);
                                    exit;
                            case 'get':
                                $response=$inventory->get_inventory($_REQUEST);
                                echo json_encode($response);
                                exit;

                        }
                    }else {
                        $response=array("status"=>0,"date"=>" No section Found ");
                        echo json_encode($response);
                        exit;
                    } 

?>

Screen shot of response

enter image description here

manikanta g
  • 298
  • 2
  • 4
  • 17

1 Answers1

0

Remove or comment all code from your script. Put only following lines in it

$section=$_REQUEST['section']; 
echo ( "section: (" . $section . ")\n");    
$section=$_POST['section']; 
echo ( "section: (" . $section . ")\n" ); 
if($section=="inventory") 
    echo ("indeed section == inventory\n"); 
if($section==="inventory") 
   echo ("indeed section === inventory\n"); 

Tell the result.

And please tell what was wrong in your code.

greenapps
  • 11,154
  • 2
  • 16
  • 19