小编Kar*_*eek的帖子

改造将多个图像上传到单个密钥

我正在使用Retrofit将图像上传到我的服务器.在这里,我需要为单个密钥上传多个图像.我已经尝试使用Postman Web客户端,它运行良好.这是一个截图.在此输入图像描述

以下是请求的键值对.
SurveyImage:[file1,file2,file3];
PropertyImage:文件
DRA:jsonBody

我试图用Retrofit做同样的事情.但图像没有上传到服务器.这是我的代码.
WebServicesAPI.java

public interface WebServicesAPI {
    @Multipart
    @POST(WebServices.UPLOAD_SURVEY)
    Call<UploadSurveyResponseModel> uploadSurvey(@Part MultipartBody.Part surveyImage, @Part MultipartBody.Part propertyImage, @Part("DRA") RequestBody dra);
}
Run Code Online (Sandbox Code Playgroud)

这是上传文件的方法.

 private void requestUploadSurvey() {
        File propertyImageFile = new File(surveyModel.getPropertyImagePath());
        RequestBody propertyImage = RequestBody.create(MediaType.parse("image/*"), propertyImageFile);
        MultipartBody.Part propertyImagePart = MultipartBody.Part.createFormData("PropertyImage", propertyImageFile.getName(), propertyImage);
        JSONObject requestBody = getRequestBody();
        RequestBody draBody = null;
        try {
            draBody = RequestBody.create(MediaType.parse("text/plain"), requestBody.toString(1));
            Log.d(TAG, "requestUploadSurvey: RequestBody : " + requestBody.toString(1));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM); …
Run Code Online (Sandbox Code Playgroud)

android multipartform-data okhttp retrofit2

23
推荐指数
3
解决办法
3万
查看次数

使用 JobScheduler 而不是 BroadcastReceiver 和 Service

我正在开发一个应用程序,当设备连接到互联网时,它将在后台按顺序将我的所有数据库记录上传到服务器。

为此,我编写了一个BroadcastReceiver监听网络连接的程序。当这个接收器被触发时,我正在启动后台服务来上传记录。

这是我的代码。

public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
       AppUtils.checkInternetConnection(context));
        //If the device has the internet connection and if there are any pending records to upload to server then start the service for uploading the records.
        if (AppUtils.checkInternetConnection(context)) {
            if (Database.getInstance().getTotalRecordsCount() > 0) {
                context.startService(new Intent(context, SurveyUploadService.class));
            }
        } else {
            context.stopService(new Intent(context, SurveyUploadService.class));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我的疑问是

1. 我可以使用JobScheduler做同样的事情吗?
2. 什么是更好的(我的或使用 JobScheduler 的)方法,为什么?

service android broadcast broadcastreceiver android-jobscheduler

4
推荐指数
1
解决办法
6602
查看次数

Android Compose Paging 3 - 通过网络调用一次性加载所有页面,无需在 LazyColumn 中滚动且无内部滚动

我正在开发一个 Android Compose 项目,其中使用 Paging 3 和 LazyColumn 来显示从网络调用获取的项目列表。Paging 3 的默认行为是根据prefetchDistance用户滚动来加载页面,但在我的例子中,它会加载大约 15 个页面,每个页面在屏幕启动时包含 10 条记录。我尝试过将其设置prefetchDistace为 10,但不起作用。

这是我的代码: ViewModel

class ShowAllNotificationsViewModel @Inject constructor(private val pagingSource: NotificationPagingSource) :
    BaseViewModel() {


    private val _uiState =
        MutableStateFlow<UIState<BaseResponseModel<NotificationResponseModel?>>>(UIState.StateLoading)
    val uiState = _uiState.asStateFlow()

    private val _isRefreshing = MutableStateFlow(false)
    val isRefreshing = _isRefreshing.asStateFlow()


    val items: Flow<PagingData<NotificationModel>> = Pager(
        config = PagingConfig(
            pageSize = 10,
            prefetchDistance = 5,
            enablePlaceholders = false,
        ),
        pagingSourceFactory = { pagingSource }
    )
        .flow
        .cachedIn(viewModelScope)

    fun refreshData() {
        pagingSource.invalidate() …
Run Code Online (Sandbox Code Playgroud)

android android-mvvm android-paging android-jetpack-compose android-paging-3

2
推荐指数
1
解决办法
138
查看次数

supportInvalidateOptionsMenu()有效吗?

我遇到了以下问题 - ActionBar在发生某些事情后,我必须在我的应用程序中更新菜单.我ActionBarActivity在appcompat库中使用.

所以,我调用supportInvalidateOptionsMenu()函数,但菜单不会更新.在调试模式中,我看到,onCreateOptionMenu方法正在调用,但菜单未更改.

难道我做错了什么?还是ActionBarActivity问题?

android

1
推荐指数
1
解决办法
6794
查看次数