将json字符串解析为模板类型

时间:2017-01-15 14:35:28

标签: android json templates

我有以下课程:

public class PostRequest<T> extends AsyncTask<String,Void,String> {
public interface OnPostCompletedListener{
    public void onCompleted(String data);
}
private OnPostCompletedListener listener;
public PostRequest(@Nullable OnPostCompletedListener listener) {
    super();
    this.listener=listener;
}
@Override
protected String doInBackground(String... params) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse resp = null;
    HttpPost httppost = new HttpPost(params[0]);
    try {
        httppost.setEntity(new StringEntity(params[1]));
        resp = httpclient.execute(httppost);
        HttpEntity ent = resp.getEntity();
        InputStream out=ent.getContent();
        byte[] resp_bytes= IOUtils.toByteArray(out);
        Log.d("net",new String(resp_bytes,"UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //
    return "";
}
@Override
protected void onPostExecute(String result) {
    if(listener!=null)
        listener.onCompleted(result);
}

}

我希望不返回String但返回类型为T的实例。但遗憾的是,通用类型不具有class属性,因此我无法执行以下操作:

    Gson gson = new Gson();
    gson.fromJson(result,T.class);

1 个答案:

答案 0 :(得分:0)

跟随下课:

public class PostRequest<T extends BaseServerResponse> extends AsyncTask<String,Void,String> {
public interface OnPostCompletedListener<T>{
    public void onCompleted(T data);
}
private OnPostCompletedListener listener;
private final Class<T> resType;
public PostRequest(@Nullable OnPostCompletedListener listener,Class<T> resType) {
    super();
    this.resType=resType;
    this.listener=listener;
}
@Override
protected String doInBackground(String... params) {
    HttpClient httpclient = new DefaultHttpClient();
    String result="";
    HttpResponse resp = null;
    HttpPost httppost = new HttpPost(params[0]);
    try {
        Log.d("request data",params[1]);
        httppost.setEntity(new StringEntity(params[1]));
        resp = httpclient.execute(httppost);
        HttpEntity ent = resp.getEntity();
        InputStream out=ent.getContent();
        byte[] resp_bytes= IOUtils.toByteArray(out);
        result=new String(resp_bytes,"UTF-8");
        Log.d("response data",result);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //
    return result;
}
@Override
protected void onPostExecute(String result) {
    Gson gson = new Gson();
    T res=null;
    try {
        res = gson.fromJson(result, resType);
    } catch(JsonSyntaxException e) {
        //
    }
    if(listener!=null)
        listener.onCompleted(res);
}
}

用法:

PostRequest<MyResponseClass> post=new PostRequest(null,MyResponseClass.class); 
post.execute("post url","post data");