62,614
社区成员
发帖
与我相关
我的任务
分享
protected <T extends Model> List<T> PostData(String serviceName, SearchInfo info)
throws ApiException {
//其中builder是一个Json字符串
Type t = new TypeToken<List<T>>() {
}.getType();
// Create a new Model from the JSON data.
List<T> list = new Gson().fromJson(
builder, t);
}
调用方法是这样调用的this.<AccessStatistics>PostData("test",info)
//其中AccessStatistics是Model的一个子类,info是SearchInfo的实例化对象
package test;
import java.util.ArrayList;
import java.util.List;
public class Node extends IModel{
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
System.out.println(new Node().<Node>test(Node.class));
}
public <T extends IModel> List<T> test(Class<T> clazz) throws InstantiationException, IllegalAccessException{
List<T> list= new ArrayList<T>();
T obj = clazz.newInstance();
list.add(obj);
System.out.println(list.get(0).getClass());
return list;
}
}
class IModel{
}
output:
class test.Node
[test.Node@1fb8ee3]
StringBuilder builder = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) {
builder.append(line);
}
out.close();
temp = builder.toString();
Type t = new TypeToken<List<T>>() {
}.getType();
// Create a new Model from the JSON data.
List<T> list = new Gson().fromJson(builder.toString(), t);
return list;
上面这段代码是PostData的方法体,inputStream是从服务器获取的流(是用来json字符串数据的)
public class AccessStatisticsInfo extends Model {
@Override
public void makeValid() {
// TODO Auto-generated method stub
}
public String SourceName;
}
上面这个是AccessStatistics类
public abstract class Model {
/**
* abstract method,subclass implements this method
* to make validation
*/
public abstract void makeValid();
上面这个是Model类
}