我尝试了很多东西,但对我没用。 Android排球加上多部分数据,Retrofit 2.0但没有任何效果。 我有一个网站服务网址,如www.---.com/business_signup/ 这是我的代码:
public class MainActivity extends AppCompatActivity {
public static final int PICK_IMAGE = 100;
Service service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn_upload);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.addInterceptor(interceptor).build();
// Change base URL to your upload server URL.
service = new Retrofit.Builder().baseUrl("http://www.app.com/").client(client).build()
//http://www.app.com/business_signup/
.create(Service.class);
if (btn != null) {
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE); }
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
android.net.Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
android.database.Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor == null)
return;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
File file = new File(filePath);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("businessLogo", file.getName(), reqFile);
RequestBody name = RequestBody.create(MultipartBody.FORM, "emailfgdf");
Log.d("THIS", data.getData().getPath());
retrofit2.Call<okhttp3.ResponseBody> req = service.postImage( body,name);
req.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(MainActivity.this, "success" + response.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(MainActivity.this, "error" , Toast.LENGTH_SHORT).show();
t.printStackTrace();
}
});
}
}
}
Service.java
interface Service {
@Multipart
@POST("/business_signup/")
Call<ResponseBody> postImage(@Part MultipartBody.Part image,
@Part("email") RequestBody name
);
}
我需要帮助。