0

在 Android Java 中,我想要一个 MyDownloadHelper 来下载并返回 JSON 数据。这是在具有不同类/对象名称的两个单独文件中工作。但是,我不能让它动态工作。

使用当前设置,我可以调用MySQLiteHelper.getRecipients();另一个活动,它将返回正确的数据。我还使用两个类(Pakbon、Recipient)来设置正确的数据。

这是我目前的来源:

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;

}

}

4

1 回答 1

0

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

}

于 2015-08-04T13:43:55.333 回答