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)