如何在类

时间:2018-01-24 06:53:08

标签: java android

我正在使用okhttp库处理POST API。一切都工作正常,除了我无法找到一种方法来显示一个简单的吐司消息成功回调。如何向用户调用toast消息,以便他知道在成功和失败回调中是否在服务器上发布了数据?

P.S下面的代码属于不属于活动类的不同类。

这是我的代码:

public DataSource(Context context) {
    this.mContext = context;
    mDbHelper = new DBHelper(mContext);
    mDatabase = mDbHelper.getWritableDatabase();
}



    post(URL, jsonData, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("FAILED", "onFailure: Failed to upload data to server");
                //here I want to show toast message
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {

                    Log.i("SUCCESSFUL", "onSuccess: data uploaded");

                    //here I want to show toast message

                } else {
                    Log.i("UN SUCCESSFUL", "onFailure: Failed to upload data to server");
                    //here I want to show toast message
                }
            }
       });

6 个答案:

答案 0 :(得分:6)

每个应用都有自己的特殊线程来运行UI对象,例如View对象;这个线程称为UI线程。只有在UI线程上运行的对象才能访问该线程上的其他对象。由于您在线程池中的线程上运行的任务未在UI线程上运行,因此它们无权访问UI对象。要将数据从后台线程移动到UI线程,请使用在UI线程上运行的Handler,或者可以使用Android实现,如此处所示。

- 案例1

 MyActivity.this.runOnUiThread(new Runnable() {
            @Override
            void run() {
               Toast.makeText(MyActivity.this,
                    "message", Toast.LENGTH_LONG).show();
            });

- 案例2

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
       Toast.makeText(MyActivity.this,
                    "message", Toast.LENGTH_LONG).show();
    }
});

如果它是主线程你会直接使用它

Toast.makeText(MyActivity.this,
                    "message", Toast.LENGTH_LONG).show();

答案 1 :(得分:1)

此回调是一个异步函数,您可以在UI线程中更改View,这样Handler将为您提供帮助。

    ....
    private final static int MSG_SUCCESS = 0x0001;
    private final static int MSG_FAIL = 0x0002;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
              case MSG_SUCCESS:
                 //Toast success
                  break;
              case MSG_FAIL:
                 //Toast fail
                  break;
              default:
                  break;
            }
        }
    };

    ......
    ......

    if (response.isSuccessful()) {
              Log.i("SUCCESSFUL", "onSuccess: data uploaded");
              handler.sendEmptyMessage(MSG_SUCCESS);
           } else {
             Log.i("UN SUCCESSFUL", "onFailure: Failed to upload data to server");
              handler.sendEmptyMessage(MSG_FAIL);
            }
     ......

答案 2 :(得分:1)

您收到错误

  

java.lang.RuntimeException:无法在线程内创建处理程序   没有调用Looper.prepare()

因为你是从工作线程调用的。您需要从主线程中调用Toast.makeText()(以及处理UI的大多数其他函数)。你可以使用一个处理程序,

 @Override
 public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {    
                    Log.i("SUCCESSFUL", "onSuccess: data uploaded");

                    context.runOnUiThread(new Runnable() {
                    public void run() {
                    Toast.makeText(context, "SUCCESSFUL", Toast.LENGTH_SHORT).show();
                     }
                  });
                }

答案 3 :(得分:0)

试试这个

Toast.makeText(YourActivity.this, "Your Message", Toast.LENGTH_SHORT).show();

<强>活动

post(URL, jsonData, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.i("FAILED", "onFailure: Failed to upload data to server");
                        //here I want to show toast message
                        Toast.makeText(YourActivity.this, "Your Message", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {

                            Log.i("SUCCESSFUL", "onSuccess: data uploaded");

                            //here I want to show toast message
                            Toast.makeText(YourActivity.this, "Your Message", Toast.LENGTH_SHORT).show();

                        } else {
                            Log.i("UN SUCCESSFUL", "onFailure: Failed to upload data to server");
                            //here I want to show toast message
                            Toast.makeText(YourActivity.this, "Your Message", Toast.LENGTH_SHORT).show();
                        }
                    }
   });

<强>片段

post(URL, jsonData, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.i("FAILED", "onFailure: Failed to upload data to server");
                        //here I want to show toast message
                        Toast.makeText(getActivity(), "Your Message", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {

                            Log.i("SUCCESSFUL", "onSuccess: data uploaded");

                            //here I want to show toast message
                            Toast.makeText(getActivity(), "Your Message", Toast.LENGTH_SHORT).show();

                        } else {
                            Log.i("UN SUCCESSFUL", "onFailure: Failed to upload data to server");
                            //here I want to show toast message
                            Toast.makeText(getActivity(), "Your Message", Toast.LENGTH_SHORT).show();
                        }
                    }
   });

修改

Handler handler = new Handler();

handler.runOnUiThread(new Runnable() {
  public void run() {
    Toast.makeText(mContext, "Your Message", Toast.LENGTH_SHORT).show();
  }
});

答案 4 :(得分:0)

试试这个

TaskActivity.this.runOnUiThread(new Runnable() {
        @Override
        void run() {
            Toast msg = Toast.makeText(TaskActivity.this,
                "message", Toast.LENGTH_LONG);
            msg.show();
        });

答案 5 :(得分:0)

只需在调用此方法时发送活动的上下文:

void methodName(Context c){

 post(URL, jsonData, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.i("FAILED", "onFailure: Failed to upload data to server");
                    //here I want to show toast message
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {

                        Log.i("SUCCESSFUL", "onSuccess: data uploaded");
                       Toast.makeText(c,"message",Toast.LENGTH_SHORT).show();
                        //here I want to show toast message

                    } else {
                        Log.i("UN SUCCESSFUL", "onFailure: Failed to upload data to server");
                        //here I want to show toast message
                    }
                }
});
}