问题
I'm trying to upload a file from my app using Retrofit to a server. Everything is almost alright and the response.isSuccess equals true, but the file is not sent. I first present my code and then I mention some results that I have used for tracing the work flow.
public class MainActivity extends Activity
{
private static int RESULT_LOAD_VIDEO = 1;
String decodableString;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn_load = (Button) findViewById(R.id.buttonLoadVideo);
btn_load.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadVideoFromGallery(btn_load);
}
});
}
/*
* PICK THE VIDEO AND EXTRACT ITS ADDRESS
*/
public void loadVideoFromGallery(View view)
{
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_VIDEO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
try {
// When a video is picked
if (requestCode == RESULT_LOAD_VIDEO && resultCode == RESULT_OK
&& null != data)
{
// Get the video from data
Uri selectedVideo = data.getData();
String[] filePathColumn = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(selectedVideo,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
decodableString = cursor.getString(columnIndex);
cursor.close();
File file = new File(decodableString);
Log.i("mok","done");
upload(file);
} else
{
Toast.makeText(this, "You haven't picked any video",
Toast.LENGTH_LONG).show();
}
} catch (Exception e)
{
e.printStackTrace();
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
/*
* UPLOAD THE SELECTED VIDEO TO THE SRVER
*/
public void upload(File file)
{
final String BASE_URL = "http://192.168.1.7/";
Retrofit retrofit = new Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Log.i("mok","here");//printed
UploadApiService service = retrofit.create(UploadApiService.class);
MediaType MEDIA_TYPE = MediaType.parse("video/mp4");
RequestBody requestBody = RequestBody.create(MEDIA_TYPE, file);
Call<ResponseBody> call = service.uploadVideo("desc", requestBody);
call.enqueue(new Callback<ResponseBody>(){
@Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit)
{
// TODO Auto-generated method stub
if (response.isSuccess())
{
Log.i("mok","S");//printed!
ResponseBody rb = response.body();
}
else
{
Log.i("mok","F");
com.squareup.okhttp.ResponseBody rb = response.errorBody();
}
}
@Override
public void onFailure(Throwable t)
{
t.printStackTrace();
Log.i("mok",t.getCause()+"");
Log.i("mok","T");
finish();
}
});
}
}
UploadApiService:
public interface UploadApiService
{
@Multipart
@PUT("api/upload.php")
Call<ResponseBody> uploadVideo(@Part("description") String description, @Part("video") RequestBody video);
}
upload.php:
<?php
ob_start();
var_dump($_FILES);
$result = ob_get_clean();
$file = fopen("test.html","w");
echo fwrite($file,$result);
fclose($file);
$uploaddir = '../uploads/';
$uploadfile = $_FILES['userfile']['name'];
$response = array();
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.$uploadfile))
{
$file = fopen("test1.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
$response["username"] = $username;
$response["result"] = "1";
echo json_encode($response);
} else
{
$file = fopen("test2.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
$response["result"] = "-1";
echo json_encode($response);
}
?>
Trace:
In LogCat, "here" and "S" are printed and as a result of calling the php page, test.html is created(its result is mentioned below), test2.html is created as well. (No file uploaded)
content of test.html:
array (size=0)
empty
Note: The php page is working properly tested using an Html form.
来源:https://stackoverflow.com/questions/33720583/response-issuccess-true-but-file-not-send