在doInBackground之后,Getter方法无法正常工作

时间:2018-05-02 23:55:22

标签: java android json android-studio android-asynctask

我尝试从一个在线JSON获取后,尝试使用一些getter方法来获取几个字符串。为了节省空间,我决定将所有内容放在一个对象中并从那里调用它们。

对象:

InventoryItem subclass = new InventoryItem(makeJSON(HOST+"Manifest/6/"+subclassHash+"/"));
            subclass.execute();
            Log.d("StringSubclass",subclass.getImageURL());
            intentHome.putExtra("SubclassImageURL",subclass.getImageURL());

问题是尽管我在doInBackground中更改了它们的值,但最后的getter方法仍会发回“”。

这就是我调用getImageURL()的方法:

 public JSONObject makeJSON(String url){
        JSONObject json = null;
        String apiKey = "36c346318fa54fc6bc659ad6321a6d41";
        try {
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            con.setRequestMethod("GET");
            con.setRequestProperty("X-API-KEY", apiKey);

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            String response = "";

            while ((inputLine = in.readLine()) != null) {
                response += inputLine;
            }

            in.close();

            JsonParser parser = new JsonParser();
            JsonObject gson = (JsonObject) parser.parse(response);
            json = new JSONObject(gson.toString());


        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }

makeJSON():

{{1}}

我检查了网址,它在Chrome中工作正常。

任何帮助将不胜感激。 我对此有点新意,请尽可能详细解释。 感谢

0 个答案:

没有答案
相关问题