gpt4 book ai didi

带有 OKHTTP 的安卓 JSON POST

转载 作者:搜寻专家 更新时间:2023-11-01 07:49:15 25 4
gpt4 key购买 nike

我正在寻找一种解决方案来使用 OKHTTP 实现 JSON-POST 请求。我有一个处理所有方法(POST、GET、PUT、DELETE)的 HTTP-Client.java 文件,在 RegisterActivity 中我想将 JSON 格式的用户数据(来自输入字段)发布到服务器。

这是我的HTTP-Client.java

    public class HttpClient{

public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");

public static OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();

@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url.host(), cookies);
}

@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url.host());
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();

public static Call post(String url, String json, Callback callback) throws IOException {
RequestBody body = RequestBody.create(JSON, json);

Request request = new Request.Builder()
.url(url)
.post(body.create(JSON, json))
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
}

...这是来自 RegisterActivity

的 onClick 部分
btnRegRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO

String registerData = "{\"email\":\"" + etRegisterEmail.getText().toString() + "\",\"password\":\"" + etRegisterPasswort.getText().toString() + "\"}";


try {
HttpClient.post(ABSOLUTE_URL, registerData, new Callback(){
@Override
public void onFailure(Call call, IOException e) {

}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String resp = response.body().string();
if (resp != null) {
Log.d("Statuscode", String.valueOf(response.code()));
Log.d("Body", response.body().string());
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});

每次我启动应用程序时,它都会在我单击注册按钮时崩溃,这是由致命的异常“android.os.NetworkOnMainThreadException”引起的

我已经读过一些关于 AsyncTask 的内容,但我不知道如何做到这一点。

最佳答案

在下面试试我的代码

  MediaType JSON = MediaType.parse("application/json; charset=utf-8");
Map<String, String> params = new HashMap<String, String>();
params.put("msisdn", "123123");
params.put("name", "your name");
JSONObject parameter = new JSONObject(param);
OkHttpClient client = new OkHttpClient();

RequestBody body = RequestBody.create(JSON, parameter.toString());
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("content-type", "application/json; charset=utf-8")
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("response", call.request().body().toString());

}

@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("response", response.body().string());
}


});

关于带有 OKHTTP 的安卓 JSON POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37752718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com