dongqiuxu2270 2017-04-11 16:10
浏览 74
已采纳

与长轮询的Android聊天

I am having trouble implementing a chat application using long polling in an android App. The search for new messages works fine, it runs in an async task with HttpUrlConnection. The problem is with sending messages. I use another async task also with HttpUrlConnection but according to the logs I've been printing the message is sent only when the search for new messages is finished, that is after 30 seconds of long polling. Here is the async tast that I'm using:

HttpPost.java

package com.roscosoft.taxifast;

import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

/**
 * Created by titin on 7/12/16.
 */

public class HttpPost extends AsyncTask<Void, Void, Boolean> {
Exception error;

public interface HttpPostInterface {
    void termino(JSONObject obj);
    void cancelo(String error);
}


public static String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, String> entry : params.entrySet()){
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}

private StringBuilder response;
private String postDataString;
private String url;
private HttpPostInterface delegate;
private int timeout;

HttpPost(String urlx, String postDataStringx, HttpPostInterface del) {
    url = urlx;
    postDataString = postDataStringx;
    delegate = del;
    timeout = 30000;
}
HttpPost(String urlx, String postDataStringx, HttpPostInterface del, int timeoutx) {
    url = urlx;
    postDataString = postDataStringx;
    delegate = del;
    timeout = timeoutx;
}


@Override
protected Boolean doInBackground(Void... params) {
    response = new StringBuilder();
    URL urlc = null;
    HttpURLConnection con = null;
    try{
        urlc = new URL(url);
    }catch (MalformedURLException e){
        Log.i("rosco", e.getMessage());
        error = e;
        return false;
    }
    try {
        con = (HttpURLConnection)urlc.openConnection();
        con.setReadTimeout(timeout);
        con.setConnectTimeout(timeout);
        con.setRequestMethod("POST");
        con.setDoInput(true);
        con.setDoOutput(true);

        OutputStream os = con.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(postDataString);

        writer.flush();
        writer.close();
        os.close();

        int responseCode = con.getResponseCode();
        if (responseCode == HttpsURLConnection.HTTP_OK){
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            while ((line = br.readLine()) != null){
                response.append(line);
            }
        }else{
            error = new Exception("Error:"+responseCode);
            return false;
        }
    } catch (Exception e) {
        Log.i("rosco", "Hubo un error al hacer el request:"+e.getMessage());
        error = e;
        return false;
    }finally {
        con.disconnect();
    }
    return true;
}

@Override
protected void onPostExecute(final Boolean success) {

    if (success) {
        try{
            if (response.toString().length() == 0){
                if(delegate != null) delegate.cancelo("No hay respuesta del servidor");
            }else{
                //Log.i("rosco", response.toString()); // solo en desarrollo
            }
            JSONObject obj = new JSONObject(response.toString());
            if(delegate != null) delegate.termino(obj);
        }catch (JSONException e){
            Log.i("rosco", "Json malformado:"+e.getMessage()+":
"+response.toString());
            if(delegate != null) delegate.cancelo(response.toString());
        }
    } else {
        if(delegate != null) delegate.cancelo(error.getMessage());
    }
}

@Override
protected void onCancelled() {
    if(delegate != null) {
        if (error == null)
            delegate.cancelo("Se cancelo el request");
        else
            delegate.cancelo(error.getMessage());
    }
}
}

The web service is made in PHP is it possible that the server waits for the first connection to finish to accept the 'send message' connection? If so, do you have any idea how to fix it? Thanks very much in advance.

  • 写回答

2条回答 默认 最新

  • douluan5444 2017-04-11 17:17
    关注

    You need to use a THREAD_POOL_EXECUTOR to execute AsyncTask. Default implementation uses a serial executor running on a single thread

    new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?