0

私はこれがかなり単純だと確信していますが、私は理解できず、それは簡単なステップです.

わかった。応答を返す 1 つの関数を実行するメソッドがあります。このメソッドは実際にファイルのアップロードを処理するため、応答に 1 秒かかります。次のメソッドでこの応答が必要です。sendPicMsg を完了してから、その応答を sendMessage に転送する必要があります。助けてください。

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

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

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

第1の方法

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

}                         


  }

2番目の方法

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 に答える