尝试发送Json对象时,应用程序崩溃

时间:2018-01-14 18:54:51

标签: java android json okhttp3

我尝试使用okhttp3将一个Json对象发送到带有android studio的服务器,当我尝试发送json时,我的应用程序总是崩溃,当应用程序说消息已发送时。另外,我需要看到我自己创建的json作为我Json工作的确认。

timeOnline

我的问题似乎出现在onResponse和onFaliure函数中。以下是我在这些函数中添加的变量的错误:http://prntscr.com/i0dhgi

错误出现在所有4个变量上,两个在onFaliure上,两个在onResponse

1 个答案:

答案 0 :(得分:0)

我在我的机器上运行你的代码,你需要做的就是这样,但要确保你在应用程序的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明确说明线程处理问题! enter image description here

这是我制作的最终解决方案 注意!你可以替换" MainActivity.this"与您的本地背景

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                
        }
    });
        }

        }
    });
}