0

I'm sure this is pretty simple but I can't figure out and it sucks I'm up on suck on (what should be) an easy step.

ok. I have a method that runs one function that give a response. this method actually handles the uploading of the file so o it takes a second to give a response. I need this response in the following method. sendPicMsg needs to complete and then forward it's response to sendMessage. Please help.

b1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        if(!uploadMsgPic.equalsIgnoreCase("")){

            Log.v("response","Pic in storage");
            sendPicMsg();
                    sendMessage();
            }else{
                    sendMessage();
                }

1st Method

public void sendPicMsg(){ 
Log.v("response", "sendPicMsg Loaded");
if(!uploadMsgPic.equalsIgnoreCase("")){

  final SharedPreferences preferences = this.getActivity().getSharedPreferences("MyPreferences", getActivity().MODE_PRIVATE);
    AsyncHttpClient client3 = new AsyncHttpClient();
    RequestParams params3 = new RequestParams();

    File file = new File(uploadMsgPic);

    try {
        File f = new File(uploadMsgPic.replace(".", "1."));
        f.createNewFile();

        //Convert bitmap to byte array
        Bitmap bitmap = decodeFile(file,400);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);


        params3.put("file", f);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    params3.put("email", preferences.getString("loggedin_user", ""));
    params3.put("webversion", "1");
    client3.post("http://*******.com/apiweb/******upload.php",params3, new AsyncHttpResponseHandler() {

      @Override
      public void onSuccess(String response) {
          Log.v("response", "Upload Complete");

          refreshChat();
        //responseString = response;
        Log.v("response","msgPic has been uploaded"+response);
        //parseChatMessages(response);
        response=picurl;
        uploadMsgPic = "";

        if(picurl!=null){
            Log.v("response","picurl is set");
        }
        if(picurl==null){
                Log.v("response", "picurl no ready");
                };

      }




  });

    sendMessage();

}                         


  }

2nd Method

public void sendMessage(){ 

  final SharedPreferences preferences = this.getActivity().getSharedPreferences("MyPreferences", getActivity().MODE_PRIVATE);
  if(preferences.getString("Username", "").length()<=0){
      editText1.setText("");
      Toast.makeText(this.getActivity(), "Please Login to send messages.", 2);
      return;
  }
    AsyncHttpClient  client = new AsyncHttpClient();
    RequestParams params = new RequestParams();
    if(type.equalsIgnoreCase("3")){
        params.put("toid",user);
         params.put("action", "sendprivate");
    }else{
        params.put("room", preferences.getString("selected_room", "Adult Lobby"));
         params.put("action", "insert");

    }
    Log.v("response", "Sending message "+editText1.getText().toString());
params.put("message",editText1.getText().toString() );
params.put("media", picurl);

 params.put("email", preferences.getString("loggedin_user", ""));
params.put("webversion", "1");


  client.post("http://peekatu.com/apiweb/*********.php",params, new AsyncHttpResponseHandler() {

      @Override
      public void onSuccess(String response) {

          refreshChat();
        //responseString = response;
        Log.v("response", response);
        //parseChatMessages(response);
        if(picurl!=null)
        Log.v("response", picurl);
      }



  });





  editText1.setText("");
    lv.setSelection(adapter.getCount() - 1);

  }
4

3 回答 3

0

不要将此 IO 和 RPC 密集型与您的客户端线程混合。单击按钮后,启动另一个处理通信的线程。

在那个线程(可能是一个单独的类)中,您发送图片并等待响应;同时将您的按钮标记为禁用以避免再次单击。然后,当您收到响应时,再次发送消息。之后,向 GUI 线程发起一个事件,启用按钮并显示消息。

于 2013-11-11T09:39:49.140 回答
0

解决这个问题的简单方法;在“onSuccess()”方法中的 sendPicMsg() 之后调用您的方法 sendMessage()

于 2013-11-11T09:50:07.833 回答
0

据我了解,您需要串行执行后台任务。在这种情况下,我所做的是使用一个扩展 AsyncTask 的类,在其构造函数中采用某种侦听器,并在 onPostExecute 中调用侦听器的回调。

一个简单的例子:

class ExampleTask<T,S,U> extends AsyncTask<T,S,U>
{
    public interface ExampleListener
    {
        public void onTaskCompleted(boolean success);
    }

    private ExampleListener mListener;

    public ExampleTask(ExampleListener listener)
    {
        mListener = listener;
    }

    ...
    @Override
    protected void onPostExecute(U result)
    {
        ...
        if (mListener != null)
        {
            mListener.onTaskCompleted(yourBooleanResult);
        }
    }
}

只需传递一个调用第二个方法的新 ExampleListener 实现。这是侦听器的实现:

ExampleListener sendMessageListener = new ExampleListener()
{
    public void onTaskCompleted(boolean success)
    {
        if(success)
            sendMessage();
    }
}
于 2013-11-11T09:54:23.347 回答