qq_27986857 2016-04-23 02:37 采纳率: 0%
浏览 1784

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

}

 ![图片说明](https://img-ask.csdn.net/upload/201604/23/1461379010_379916.png)
  • 写回答

1条回答 默认 最新

  • molashaonian 2016-04-23 06:14
    关注
    @Override
    protected String doInBackground(String... params) {
                return result;
    }
    
            @Override
    protected void onPostExecute(String result) {
        }
        在doInBackground方法中要return,才会去到onPostExecute方法,返回结果就是onPostExecute的参数
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)