Skip to content

Commit

Permalink
BodyParam 类使用kotlin编写
Browse files Browse the repository at this point in the history
  • Loading branch information
liujingxing committed Dec 20, 2020
1 parent 8391da0 commit 1014c0a
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.net.Uri;

import rxhttp.wrapper.annotations.Nullable;
import rxhttp.wrapper.param.BodyParam;

import java.io.File;
Expand All @@ -20,54 +21,54 @@ public class RxHttpBodyParam extends RxHttpAbstractBodyParam<BodyParam, RxHttpBo
public RxHttpBodyParam(BodyParam param) {
super(param);
}

public RxHttpBodyParam setBody(RequestBody requestBody) {
param.setBody(requestBody);
return this;
}

public <T> RxHttpBodyParam setJsonBody(T object) {
param.setJsonBody(object);
return this;
}

public RxHttpBodyParam setBody(MediaType mediaType, String content) {
param.setBody(mediaType, content);

public RxHttpBodyParam setBody(String content, @Nullable MediaType mediaType) {
param.setBody(content, mediaType);
return this;
}

public RxHttpBodyParam setBody(MediaType mediaType, ByteString content) {
param.setBody(mediaType, content);
public RxHttpBodyParam setBody(ByteString content, @Nullable MediaType mediaType) {
param.setBody(content, mediaType);
return this;
}

public RxHttpBodyParam setBody(MediaType mediaType, byte[] content) {
param.setBody(mediaType, content);
public RxHttpBodyParam setBody(byte[] content, @Nullable MediaType mediaType) {
param.setBody(content, mediaType);
return this;
}

public RxHttpBodyParam setBody(MediaType mediaType, byte[] content, int offset, int byteCount) {
param.setBody(mediaType, content, offset, byteCount);
public RxHttpBodyParam setBody(byte[] content, @Nullable MediaType mediaType, int offset, int byteCount) {
param.setBody(content, mediaType, offset, byteCount);
return this;
}

public RxHttpBodyParam setBody(File file) {
param.setBody(file);
return this;
}

public RxHttpBodyParam setBody(MediaType mediaType, File file) {
param.setBody(mediaType, file);
public RxHttpBodyParam setBody(File file, @Nullable MediaType mediaType) {
param.setBody(file, mediaType);
return this;
}

public RxHttpBodyParam setBody(Context context, Uri uri) {
param.setBody(context, uri);
return this;
}

public RxHttpBodyParam setBody(Context context, MediaType contentType, Uri uri) {
param.setBody(context, contentType, uri);

public RxHttpBodyParam setBody(Context context, Uri uri, @Nullable MediaType contentType) {
param.setBody(context, uri, contentType);
return this;
}

public <T> RxHttpBodyParam setJsonBody(T object) {
param.setJsonBody(object);
return this;
}
}
51 changes: 26 additions & 25 deletions rxhttp-compiler/src/main/java/com/rxhttp/compiler/ClassHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ object ClassHelper {
import android.content.Context;
import android.net.Uri;
import rxhttp.wrapper.annotations.Nullable;
import rxhttp.wrapper.param.BodyParam;
import java.io.File;
Expand All @@ -1076,54 +1077,54 @@ object ClassHelper {
public RxHttpBodyParam(BodyParam param) {
super(param);
}
public RxHttpBodyParam setBody(RequestBody requestBody) {
param.setBody(requestBody);
return this;
}
public <T> RxHttpBodyParam setJsonBody(T object) {
param.setJsonBody(object);
return this;
}
public RxHttpBodyParam setBody(MediaType mediaType, String content) {
param.setBody(mediaType, content);
public RxHttpBodyParam setBody(String content, @Nullable MediaType mediaType) {
param.setBody(content, mediaType);
return this;
}
public RxHttpBodyParam setBody(MediaType mediaType, ByteString content) {
param.setBody(mediaType, content);
public RxHttpBodyParam setBody(ByteString content, @Nullable MediaType mediaType) {
param.setBody(content, mediaType);
return this;
}
public RxHttpBodyParam setBody(MediaType mediaType, byte[] content) {
param.setBody(mediaType, content);
public RxHttpBodyParam setBody(byte[] content, @Nullable MediaType mediaType) {
param.setBody(content, mediaType);
return this;
}
public RxHttpBodyParam setBody(MediaType mediaType, byte[] content, int offset, int byteCount) {
param.setBody(mediaType, content, offset, byteCount);
public RxHttpBodyParam setBody(byte[] content, @Nullable MediaType mediaType, int offset, int byteCount) {
param.setBody(content, mediaType, offset, byteCount);
return this;
}
public RxHttpBodyParam setBody(File file) {
param.setBody(file);
return this;
}
public RxHttpBodyParam setBody(MediaType mediaType, File file) {
param.setBody(mediaType, file);
public RxHttpBodyParam setBody(File file, @Nullable MediaType mediaType) {
param.setBody(file, mediaType);
return this;
}
public RxHttpBodyParam setBody(Context context, Uri uri) {
param.setBody(context, uri);
return this;
}
public RxHttpBodyParam setBody(Context context, MediaType contentType, Uri uri) {
param.setBody(context, contentType, uri);
public RxHttpBodyParam setBody(Context context, Uri uri, @Nullable MediaType contentType) {
param.setBody(context, uri, contentType);
return this;
}
public <T> RxHttpBodyParam setJsonBody(T object) {
param.setJsonBody(object);
return this;
}
}
Expand Down
123 changes: 60 additions & 63 deletions rxhttp/src/main/java/rxhttp/wrapper/param/BodyParam.kt
Original file line number Diff line number Diff line change
@@ -1,89 +1,86 @@
package rxhttp.wrapper.param;
package rxhttp.wrapper.param

import android.content.Context;
import android.net.Uri;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.ByteString;
import rxhttp.wrapper.entity.UriRequestBody;
import rxhttp.wrapper.utils.BuildUtil;
import android.content.Context
import android.net.Uri
import okhttp3.MediaType
import okhttp3.RequestBody
import okio.ByteString
import rxhttp.wrapper.entity.UriRequestBody
import rxhttp.wrapper.utils.BuildUtil
import java.io.File

/**
* User: ljx
* Date: 2019-09-11
* Time: 11:52
*
* @param url request url
* @param method [Method.POST]、[Method.PUT]、[Method.DELETE]、[Method.PATCH]
*/
public class BodyParam extends AbstractBodyParam<BodyParam> {

private RequestBody requestBody;

/**
* @param url request url
* @param method {@link Method#POST}、{@link Method#PUT}、{@link Method#DELETE}、{@link Method#PATCH}
*/
public BodyParam(String url, Method method) {
super(url, method);
}
class BodyParam(
url: String,
method: Method,
) : AbstractBodyParam<BodyParam>(url, method) {

public BodyParam setBody(RequestBody requestBody) {
this.requestBody = requestBody;
return this;
}

public <T> BodyParam setJsonBody(T object) {
this.requestBody = convert(object);
return this;
}

public BodyParam setBody(MediaType mediaType, String content) {
requestBody = RequestBody.create(mediaType, content);
return this;
}
private var requestBody: RequestBody? = null

public BodyParam setBody(MediaType mediaType, ByteString content) {
requestBody = RequestBody.create(mediaType, content);
return this;
fun setBody(requestBody: RequestBody): BodyParam {
this.requestBody = requestBody
return this
}

public BodyParam setBody(MediaType mediaType, byte[] content) {
requestBody = RequestBody.create(mediaType, content);
return this;
@JvmOverloads
fun setBody(content: String, mediaType: MediaType? = null): BodyParam {
requestBody = RequestBody.create(mediaType, content)
return this
}

public BodyParam setBody(MediaType mediaType, byte[] content, int offset, int byteCount) {
requestBody = RequestBody.create(mediaType, content, offset, byteCount);
return this;
@JvmOverloads
fun setBody(content: ByteString, mediaType: MediaType? = null): BodyParam {
requestBody = RequestBody.create(mediaType, content)
return this
}

public BodyParam setBody(File file) {
return setBody(BuildUtil.getMediaType(file.getName()), file);
@JvmOverloads
fun setBody(
content: ByteArray,
mediaType: MediaType? = null,
offset: Int = 0,
byteCount: Int = content.size,
): BodyParam {
requestBody = RequestBody.create(mediaType, content, offset, byteCount)
return this
}

public BodyParam setBody(MediaType mediaType, File file) {
requestBody = RequestBody.create(mediaType, file);
return this;
@JvmOverloads
fun setBody(
file: File,
mediaType: MediaType? = BuildUtil.getMediaType(file.name),
): BodyParam {
requestBody = RequestBody.create(mediaType, file)
return this
}

public BodyParam setBody(Context context, Uri uri) {
requestBody = new UriRequestBody(context, uri);
return this;
@JvmOverloads
fun setBody(
context: Context,
uri: Uri,
contentType: MediaType? = null,
): BodyParam {
requestBody = UriRequestBody(context, uri, contentType)
return this
}

public BodyParam setBody(Context context, MediaType contentType, Uri uri) {
requestBody = new UriRequestBody(context, uri, contentType);
return this;
fun <T> setJsonBody(any: T): BodyParam {
requestBody = convert(any)
return this
}

@Override
public RequestBody getRequestBody() {
return requestBody;
override fun getRequestBody(): RequestBody {
return requestBody!!
}

@Override
public BodyParam add(String key, Object value) {
return this;
override fun add(key: String, value: Any): BodyParam {
return this
}
}
}

0 comments on commit 1014c0a

Please sign in to comment.