我正在尝试将用户数据(例如:姓名,电话,电子邮件)和文件附件从android传递到php服务器,数据将保存在mysql中。当我使用模拟器运行时,数据插入成功(使用multipartEntity),但是使用实际的手机时,它会给出错误,就好像参数没有说明一样。
NextActivity.java中的:
private class RegisterPSJobActivity extends AsyncTask<String, Void, String> {
// private void registerPSJob(){
@SuppressLint("SimpleDateFormat")
@Override
protected String doInBackground(String... arg0) {
String svcCtr = spinnerSvc.getSelectedItem().toString();
String product = spinnerProduct.getSelectedItem().toString();
String name = (String) arg0[0];
String phone = (String) arg0[1];
String email = (String) arg0[2];
String imei = (String) arg0[3];
String username = (String) arg0[4];
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String dateNow = dateFormat.format(date);
try {
HttpClient httpclient = new DefaultHttpClient();
/*
* HttpPost httppost = new HttpPost(
* "http://192.168.43.140/ps/sendJobs.php");
*/
HttpPost httppost = new HttpPost(
URL + "sendJobs.php");
MultipartEntity mEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE,null,Charset.forName("UTF-8"));
mEntity.addPart("dealer", new StringBody(dealer));
mEntity.addPart("svcCtr", new StringBody(svcCtr));
mEntity.addPart("product", new StringBody(product));
mEntity.addPart("name", new StringBody(name));
mEntity.addPart("phone", new StringBody(phone));
mEntity.addPart("email", new StringBody(email));
mEntity.addPart("imei", new StringBody(imei));
mEntity.addPart("date", new StringBody(dateNow));
mEntity.addPart("createdBy", new StringBody(username));
mEntity.addPart("timeUpdated", new StringBody(dateNow));
// Thread.sleep(1000);
//System.out.println(fileUri.getPath());
mEntity.addPart("attach1", new FileBody(new File(fileUri.getPath()), "application/octet-stream","UTF-8"));
mEntity.addPart("attach2",new FileBody(new File(fileUriPOD.getPath()), "application/octet-stream","UTF-8"));
httppost.setEntity(mEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
} catch (Exception ex) {
Log.e("Fail 1", ex.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
} catch (Exception ex1) {
Log.e("Fail 2", ex1.toString());
}
try {
Log.i("Info", result);
JSONObject json_data = new JSONObject(result);
code = (json_data.getInt("code"));
} catch (Exception e) {
Log.e("Fail 3", e.toString());
}
return Integer.toString(code);
}
SendJobs.php:
<?php
$con=mysqli_connect("localhost","root","","psapp");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$imei = $_POST['imei'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$target_path = "upload/";
$target_path2 = "upload/";
$copy_path = "D:/apache-tomcat-7.0.57/webapps/PS/upload/";
$server_ip = gethostbyname(gethostname());
$file_upload_url = 'http://' . $server_ip . '/' . 'ps' . '/' . $target_path;
if (isset($_FILES['attach1']['name']) && isset($_FILES['attach2']['name'])) {
$target_path = $target_path . basename($_FILES['attach1']['name']);
$target_path2 = $target_path2 . basename($_FILES['attach2']['name']);
}
if (!move_uploaded_file($_FILES['attach1']['tmp_name'], $target_path)) {
// make error flag true
echo true;
echo 'Could not move the file!';
}
if (!move_uploaded_file($_FILES['attach2']['tmp_name'], $target_path2)) {
// make error flag true
echo true;
echo 'Could not move the file!';
}
copy('C:/wamp/www/ps/upload/'.$_FILES['attach1']['name'],$copy_path.$_FILES['attach1']['name']);
copy('C:/wamp/www/ps/upload/'.$_FILES['attach2']['name'],$copy_path.$_FILES['attach2']['name']);
$attach1 = $_FILES['attach1']['name'];
$attach2 = $_FILES['attach2']['name'];
$dateNow = $_POST['date'];
$timeUpdated = $_POST['timeUpdated'];
$createdBy = $_POST['createdBy'];
$dealerLocation = $_POST['dealer'];
$sc = $_POST['svcCtr'];
$product = $_POST['product'];
$randomNo = rand();
$flag['code']=0;
header("Content-type: image/jpeg");
if($result = mysqli_query($con,"INSERT INTO PSJOB (DEALERLOCATION, SERVICEORDER, SERVICECENTER, PRODUCT, IMEINO, NAME, PHONE, EMAIL, ATTACHMENT1, ATTACHMENT2, REGISTRATIONDATETIME, CREATEDBY, TIMEUPDATED, STATUS)
VALUES('$dealerLocation', '$randomNo', '$sc', '$product', '$imei', '$name', '$phone', '$email', '$attach1', '$attach2', '$dateNow', '$createdBy', '$timeUpdated', 'REGISTRATION')")){
$flag['code']=1;
#echo"hi";
}
print(json_encode($flag));
mysqli_close($con);
错误消息:(好像php代码找不到名称,imei或参数的其余部分) sendJobs行中未定义的索引名称... sendJobs行中未定义的索引imei ...