尝试发送 Json 对象时应用程序崩溃
App crashes when tries to send Json object
我正在尝试使用 okhttp3 将 Json 对象发送到带有 android studio 的服务器,但当我尝试发送 json 时,我的应用程序总是崩溃,当应用程序显示消息已发送时。此外,我需要查看我自己创建的 json 的响应,以确认我的 Json 有效。
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
Log.e("TAG", "Failed sending message!");
Toast.makeText(MainActivity.this,"Failed sending message",Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(okhttp3.Call call, Response response) throws IOException {
Log.d("TAG", "Message sent successfully!");
Log.d("TAG", response.body().string());
Toast.makeText(MainActivity.this,"Message sent successfully!",Toast.LENGTH_LONG).show();
}
});
}
我的问题似乎出现在 onResponse 和 onFaliure 函数中。这是我在这些函数中输入的变量的错误:http://prntscr.com/i0dhgi
错误出现在所有 4 个变量上,两个在 onFaliure 中,两个在 onResponse 中
我 运行 你的代码在我的机器上你需要做的是类似的事情,但确保你的应用程序中有这个 build.gradle
compile 'com.android.support:support-annotations:20.0.0'
如果您使用的是旧 android 工作室版本。新版本使用内置注释处理器制作项目
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure( @NonNull okhttp3.Call call,@NonNull IOException e) {
Log.e("TAG", "Failed sending message!");
//using a toast means updating the UI thread from back thread you have to call Content.runOnUiThread(new Runnable) to sync with the UI thread.
//Toast.makeText(MainActivity.this,"Failed sending message",Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(@NonNull okhttp3.Call call,@NonNull Response response) throws IOException {
Log.d("TAG", "Message sent successfully!");
Log.d("TAG", response.body().string());
//Toast.makeText(MainActivity.this,"Message sent successfully!",Toast.LENGTH_LONG).show();
}
});
}
看看我 运行 带有虚拟值的代码的图片,然后看到 logcat 清楚地说明了线程处理问题!
这是我为您制定的最终解决方案,可以解决问题
注意!您可以将 "MainActivity.this" 替换为您当地的 Context
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure( @NonNull okhttp3.Call call,@NonNull IOException e) {
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Handle UI here
// Toast anything you like here//
}
});
}
@Override
public void onResponse(@NonNull okhttp3.Call call,@NonNull Response response) throws IOException {
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Handle UI here
//happy on Response Toast here
}
});
}
}
});
}
我正在尝试使用 okhttp3 将 Json 对象发送到带有 android studio 的服务器,但当我尝试发送 json 时,我的应用程序总是崩溃,当应用程序显示消息已发送时。此外,我需要查看我自己创建的 json 的响应,以确认我的 Json 有效。
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
Log.e("TAG", "Failed sending message!");
Toast.makeText(MainActivity.this,"Failed sending message",Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(okhttp3.Call call, Response response) throws IOException {
Log.d("TAG", "Message sent successfully!");
Log.d("TAG", response.body().string());
Toast.makeText(MainActivity.this,"Message sent successfully!",Toast.LENGTH_LONG).show();
}
});
}
我的问题似乎出现在 onResponse 和 onFaliure 函数中。这是我在这些函数中输入的变量的错误:http://prntscr.com/i0dhgi
错误出现在所有 4 个变量上,两个在 onFaliure 中,两个在 onResponse 中
我 运行 你的代码在我的机器上你需要做的是类似的事情,但确保你的应用程序中有这个 build.gradle
compile 'com.android.support:support-annotations:20.0.0'
如果您使用的是旧 android 工作室版本。新版本使用内置注释处理器制作项目
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure( @NonNull okhttp3.Call call,@NonNull IOException e) {
Log.e("TAG", "Failed sending message!");
//using a toast means updating the UI thread from back thread you have to call Content.runOnUiThread(new Runnable) to sync with the UI thread.
//Toast.makeText(MainActivity.this,"Failed sending message",Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(@NonNull okhttp3.Call call,@NonNull Response response) throws IOException {
Log.d("TAG", "Message sent successfully!");
Log.d("TAG", response.body().string());
//Toast.makeText(MainActivity.this,"Message sent successfully!",Toast.LENGTH_LONG).show();
}
});
}
看看我 运行 带有虚拟值的代码的图片,然后看到 logcat 清楚地说明了线程处理问题!
这是我为您制定的最终解决方案,可以解决问题 注意!您可以将 "MainActivity.this" 替换为您当地的 Context
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure( @NonNull okhttp3.Call call,@NonNull IOException e) {
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Handle UI here
// Toast anything you like here//
}
});
}
@Override
public void onResponse(@NonNull okhttp3.Call call,@NonNull Response response) throws IOException {
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//Handle UI here
//happy on Response Toast here
}
});
}
}
});
}