我正在尝试在Php服务器中上传文件。服务器接受包含UserID,密码和视频文件的请求。
以下是服务器端代码。
public function uploadVideoAction(Request $request) {
$tranlator = $this->get('translator');
$sUserName = $request->request->get('username');
$passWord = $request->request->get('password');
if(empty($sUserName) || empty($passWord) || empty($_FILES['video_profile_candidate'])) {
$view = View::create()
->setStatusCode(400)
->setData(array(
'message' => $tranlator->trans('Invalid parameters')
));
return $this->get('fos_rest.view_handler')->handle($view);
}
当我尝试通过 HttpPost 从 android 发送请求时,收到"无效参数"错误。 谁能帮我。
这是我的安卓代码
JSONObject jsonObject = new JSONObject();
try
{
jsonObject.put(CommonString.LOGIN_USER_NAME,"username");
jsonObject.put(CommonString.LOGIN_PASSWORD,"####");
System.out.println("object "+jsonObject);
}
catch (JSONException e)
{
e.printStackTrace();
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("url");
Log.i(TAG, "request :url");
httpost.setHeader("Content-type", "application/x-www-urlendcode");
File file = new File(CommonString.PATH+File.separator+CommonString.VIDEO_SEND_NAME);
if(file.exists())
{
System.out.println("File path "+file.getAbsolutePath());
}
try
{
FileBody filebody=new FileBody(file);
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("'password", new StringBody("password"));
multipartEntity.addPart("'username", new StringBody("username"));
multipartEntity.addPart("'video_profile_candidate", filebody);
httpost.setEntity(multipartEntity);
HttpResponse httpresponse = httpClient.execute(httpost);
System.out.println("Hiii "+ httpresponse.getStatusLine().getStatusCode());
Log.i(TAG, "request"+ httpresponse.getStatusLine().getStatusCode());
}
catch (Exception e)
{
Log.i(TAG, "Exception "+e.getMessage());
System.out.println("Exception "+e.getMessage());
}
System.out.println("text "+response);
你确定"application/x-www-urlendcode"吗?也许"application/x-www-form-urlencoded"?