AndroidStudio 调用FastAPI 的GET可以正常访问·就是拿不到数据,找了一圈说是编码问题。
AndroidStudio 调用FastAPI 的GET的POST就完全不对了,一直报 422 Unprocessable Entity,表单的方式也试过了·返回 307 Temporary Redirect。
求帮忙·写个示例
下面贴出他AndroidStudio的代码。
private void sendGet(String title, String text, String packageName, String remark, MessageModel model) {
OkHttpClient mOkHttpClient = new OkHttpClient();
StringBuilder sb = new StringBuilder(model.getUrl());
sb.append("?").append("title=").append(title).append("&text=").append(text).append("&packageName=").append(packageName).append("&remark=").append(remark).append("&date=").append(Constant.FORMATTER.format(new Date()));
final Request request = new Request.Builder()
.url(sb.toString())
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
//如果请求失败了...
Log.i("自定义请求","发送自定义请求失败!");
}
@Override
public void onResponse(Response response) throws IOException {
Log.i("自定义请求","发送自定义请求成功!"+response);
}
});
}
private void sendPostOrOther(String title, String text, String packageName, String remark, MessageModel model) {
OkHttpClient client = new OkHttpClient();
//构建表单参数
FormEncodingBuilder builder = new FormEncodingBuilder();
//添加请求体
RequestBody requestBody = builder
.add("title", title)
.add("text", text)
.add("packageName", packageName)
.add("remark", remark)
.add("date", Constant.FORMATTER.format(new Date()))
.build();
Request request = new Request.Builder()
.url(model.getUrl())
.post(requestBody)
.build();
//异步请求
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.i("自定义请求","发送自定义请求失败!");
}
@Override
public void onResponse(Response response) throws IOException {
Log.i("自定义请求","发送自定义请求成功!");
}
});
}