[英]Java passing className + object to generic method
In Android Java I want a MyDownloadHelper to download and return JSON data. 在Android Java中,我希望MyDownloadHelper下载并返回JSON数据。 This is working in two separate files with different class/object names.
这是在两个具有不同类/对象名称的单独文件中工作的。 However, I can't get this to work dynamically.
但是,我无法使它动态地工作。
With the current setup, I can call MySQLiteHelper.getRecipients();
使用当前设置,我可以调用
MySQLiteHelper.getRecipients();
in another activity and it will return the correct data. 在另一个活动中,它将返回正确的数据。 I am also using two classes (Pakbon, Recipient) for setting the correct data.
我还使用两个类(Pakbon,收件人)来设置正确的数据。
This is my current source: 这是我当前的来源:
public class MyDownloadHelper {
private static final int timeout = 10000;
private Class<? extends Object[]> cls;
private static final String API_SERVER = "http://www.***.nl/json/";
private Object[] obj;
public MyDownloadHelper(){
}
protected Recipient[] getRecipients() {
try {
//Recipient[] recipients = getInstance(Recipient[].class);
Recipient[] recipients = this.download(Recipient[].class, API_SERVER + "getRecipients");
return recipients;
} finally {
return null;
}
}
protected Pakbon[] getPackingSlips() {
try {
Pakbon[] pakbon = this.download(Pakbon[].class, API_SERVER + "getPackingSlips");
return pakbon;
} finally {
return null;
}
}
private <T> Object[] download(Class<T> a, String url){
HttpURLConnection c = null;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
Gson gson = new Gson();
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
Object[] objectData = gson.fromJson(br, a);
return gson.fromJson(br, cls);
}
} catch (IOException ex) {
} finally{
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
} }
Solution with the help of Selvin. 在Selvin的帮助下的解决方案。
public class MyDownloadHelper {
private static final int timeout = 10000;
private Class<? extends Object[]> cls;
protected static final String API_SERVER = "http://www.translog.nl/json/";
private Object[] obj;
public MyDownloadHelper(){
}
protected <T> T download(Class<T> a, String url) throws Exception {
HttpURLConnection c = null;
try {
URL u = new URL(API_SERVER + url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
Gson gson = new Gson();
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
return (T)gson.fromJson(br, a);
//return gson.fromJson(br, cls);
}
} catch (IOException ex) {
} finally{
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.