2 qq 27986857 qq_27986857 于 2016.04.23 10:37 提问

AsyncTask的onPostExecute()没有执行,DoInBackground执行了。

package com.kisentech.cloudrun.domain;

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

import com.kisentech.cloudrun.listener.OnLoadShareDataListener;
import com.kisentech.cloudrun.util.ShareInformation;

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

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ShareData extends AsyncTask> {

//服务端地址
private  String urlPath;
//数据集合
private  List<ShareInformation>shareInformations;
//回调接口
private OnLoadShareDataListener onLoadShareDataListener;
//客户端与服务端的链接通道
private HttpURLConnection httpURLConnection=null;

public ShareData(String urlPath){
    this.urlPath=urlPath;
    shareInformations=new ArrayList<>();
}

/**
 * 根剧url地址获取客户端与服务端之间的流通道
 * @param urlPath 服务端地址
 */
public InputStream getInputStream(String urlPath){
    Log.v("test","getInputStream");
    InputStream inputStream=null;

    try {
        URL url=new URL(urlPath);

        httpURLConnection= (HttpURLConnection) url.openConnection();

        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setConnectTimeout(8000);
        httpURLConnection.setDoInput(true);

// int requestCode=httpURLConnection.getResponseCode();
// if(requestCode!=200){
// Log.v("test","connect lose!");
// }else{
// Log.v("test","connecting");
// }

        inputStream=httpURLConnection.getInputStream();
    } catch (Exception e) {
        Log.v("test","getInputStream发生异常");
        e.printStackTrace();
    }
    return inputStream;
}

/**
 * 将json对象从流中读出转化为字符串
 * @param inputStream 客户端与服务端之间的流通道
 * @return json字符串
 */
private String jsonToString(InputStream inputStream){
    Log.v("test","jsonToString");
    String jsonString="";
    int length;
    byte[]buff=new byte[1024];
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    try {
        while ((length = inputStream.read(buff)) != -1) {
            baos.write(buff,0,length);
        }
        jsonString=new String(baos.toByteArray());
    }catch (Exception e){
        e.printStackTrace();
        Log.v("test","jsonString發生異常");
    }finally {
        if(httpURLConnection!=null){
            httpURLConnection.disconnect();
        }
    }
    return jsonString;
}

/**
 * 解析从服务器中传来的json字符传对象
 * @param jsonString json字符串

 */
private void parseJsonString(String jsonString){
    Log.v("test","pareseJsonString");
    try {
        JSONArray jsonArray=new JSONArray(jsonString);
        for (int i=0;i<jsonArray.length();i++){
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            ShareInformation shareInformation=setShareInformation(jsonObject);
            shareInformations.add(shareInformation);
        }
    } catch (JSONException e) {
        e.printStackTrace();
        Log.v("test","parseJsonString發生異常");
    }
}

/**
 * 为shareInformation赋值
 * @param jsonObject 信息的来源
 * @return 赋值后的ShareInformation对象
 * @throws JSONException
 */
private ShareInformation setShareInformation(JSONObject jsonObject) throws JSONException {
    Log.v("test","setShare");
    ShareInformation shareInformation=new ShareInformation();

    shareInformation.setHeadImageId(jsonObject.getInt("headImage"));
    shareInformation.setName(jsonObject.getString("name"));
    shareInformation.setTextContent(jsonObject.getString("textContent"));
    shareInformation.setLikeImage(jsonObject.getInt("likeImage"));
    shareInformation.setCommentImage(jsonObject.getInt("commentImage"));

    return shareInformation;
}


/**
 * 为ShareData添加监听
 * @param onLoadShareDataListener
 */
public void setOnLoadShareDataListener(OnLoadShareDataListener onLoadShareDataListener){
    this.onLoadShareDataListener=onLoadShareDataListener;
}

@Override
protected List<ShareInformation> doInBackground(Void... params) {
    Log.v("test","doInBack");
    InputStream inputStream=getInputStream(urlPath);

    String jsonString=jsonToString(inputStream);

    parseJsonString(jsonString);

    return shareInformations;
}

@Override
protected void onPostExecute(List<ShareInformation> shareInformations) {
    Log.d("test","onPost");
    for(ShareInformation s:shareInformations)
        Log.v("test",s.toString());
    if (shareInformations!=null&&onLoadShareDataListener!=null){
        onLoadShareDataListener.loadShareInformationData(shareInformations);
    }
}

}

 ![图片说明](http://img.ask.csdn.net/upload/201604/23/1461379010_379916.png)

2个回答

CSDNXIAOD
CSDNXIAOD   2016.04.23 10:42

AsyncTask中执行完doInBackground不执行onPostExecute
AsyncTask的doInBackground不执行
AsyncTask onPostExecute 未执行问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

molashaonian
molashaonian   2016.04.23 14:14
@Override
protected String doInBackground(String... params) {
            return result;
}

        @Override
protected void onPostExecute(String result) {
    }
    在doInBackground方法中要return,才会去到onPostExecute方法,返回结果就是onPostExecute的参数
qq_27986857
qq_27986857 我返回了的啊,return shareInformations;
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!