我正在尝试实现带有多个图像的示例表单提交到php服务器。下面是我用来实现的代码片段
提交时
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);
}
}
应用程序似乎发送了后期数据,但下面的服务器上没有收到的数据是php脚本
<?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;
}
?>
回复的屏幕截图
答案 0 :(得分:0)
删除或评论脚本中的所有代码。只在其中加入以下行
$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");
告诉结果。
请告诉您的代码有什么问题。