A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

//Api

public interface ILoginApi {

    @GET("login")
    Call<LoginBean> loginByMap(@Query("mobile") String mobile, @Query("password") String password);

    @GET("login")
    Call<LoginBean> loginByMap(@QueryMap Map<String ,String> map);


    @GET("{abc}")
    Call<LoginBean> loginByPath(@Path("abc") String abc, @Query("mobile") String mobile, @Query("password") String password);

    @GET()
    Call<LoginBean> loginByUrl(@Url String url, @Query("mobile") String mobile, @Query("password") String password);


    @FormUrlEncoded
    @POST("login")
    Call<LoginBean> loginPost(@Field("mobile") String mobile, @Field("password") String password);


    @Multipart
    @POST("file/upload")
    Call<UploadBean> upLoadPhoto(@Query("uid") String mobile, @Part MultipartBody.Part part);

}




//RetrofitManager

public class RetrofitManager {
    private static String BASE_URL = "https://www.zhaoapi.cn/";
    private Retrofit mRetrofit;
    private static class SingleHolder {
        private static final RetrofitManager _INSTANT = new RetrofitManager(BASE_URL);
    }
    public static RetrofitManager getDefault() {
        return SingleHolder._INSTANT;
    }
    private RetrofitManager(String baseUrl) {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(buildOkhttpClinet())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }
    private OkHttpClient buildOkhttpClinet() {
        return new OkHttpClient.Builder()
                .readTimeout(5, TimeUnit.SECONDS)
                .build();
    }

    public <T> T create(Class<T> Clazz) {
        return mRetrofit.create(Clazz);
    }
}



//MainActivity

public class MainActivity extends AppCompatActivity {

    private static final String BASE_URL = "https://www.zhaoapi.cn/user/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
//点击事件
    public void login(View view) {
        ILoginApi iLoginApi = RetrofitManager.getDefault().create(ILoginApi.class);
//        final Call<LoginBean> login = iLoginApi.loginPost("15501186523", "123456");

        File file = getResourseFile();
        //RequestBody封装了文件和文件的类型
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
        // MultipartBody.Part封装了接受的key和文件名字和RequestBody
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
        Call<UploadBean> loginBeanCall = iLoginApi.upLoadPhoto("71", part);

        loginBeanCall.enqueue(new Callback<UploadBean>() {
            @Override
            public void onResponse(Call<UploadBean> call, Response<UploadBean> response) {
                UploadBean uploadBean = response.body();

                if (uploadBean != null && "0".equals(uploadBean.getCode())) {
                    Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();

                } else {
                    Log.e("tag", "失败" + uploadBean.toString());
                    Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UploadBean> call, Throwable t) {
                Log.e("tag", "失败" + t.getMessage());
                Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private File getResourseFile() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        File file = new File(getFilesDir().getAbsolutePath());
        if (!file.exists()) {
            file.mkdirs();
        }
        File file1 = new File(file, "photo.png");
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file1);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            fileOutputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file1;
    }
}


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马