【问题标题】:JSON Spring Framework AndroidJSON Spring 框架 Android
【发布时间】:2015-10-23 13:09:44
【问题描述】:

晚上好,

我在 Android Studio 中学习 Android 开发,我是原始 C# 开发人员 我现在将json解析到我的android应用程序

在我的 Events.java 中使用以下代码

  public static class HttpRequestTask extends AsyncTask<Void, Void, Events> {
    @Override
    protected Events doInBackground(Void... params) {
        try {

            String url = "http://example.com/Example.ashx?Onderdeel=Feeds&AuthToken=" + Core.AuthToken;
            Log.e("Ophalen Events", url);

            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            Events events = restTemplate.getForObject(url, Events.class);

            // Log.e("EVENTS:", events.getContent());

            return events;
        } catch (Exception e) {
            Log.e("MainActivity", e.getMessage(), e);
        }
        return null;
    }
}

我在我的 onCreateView 中调用了这段代码 喜欢

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
 View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);

new Events.HttpRequestTask().execute();


 return rootView;
}

问题是我想返回对象/类事件

喜欢事件 e = Events.HttpRequestTask().execute(); 而比 e = 我的填充对象。

自从我是新手以来,是否有人有任何提示或想法 :) 非常喜欢

问候, 斯蒂芬

【问题讨论】:

    标签: java android spring gradle frameworks


    【解决方案1】:

    已经用 SpringFramework 完成了。

    【讨论】:

      【解决方案2】:

      调用execute()会返回AsyncTask本身,将响应传递给

      Event getEvent(String response){
      Event e=new Event();
      // parse the data & set the model data.
      return e;
      }
      

      用于标准 Android 方式编码请求。 对于网络请求,请使用 volley 谷歌库。要解析 JSON,请使用 GSON 库。 对于简单的例子,只需通过下面的链接https://stackoverflow.com/a/24433916/3187349 在 android fallow 中,MVP 架构通过本教程。 http://antonioleiva.com/mvp-android/

      【讨论】:

        【解决方案3】:

        调用execute()会返回AsyncTask本身,你需要在它上面调用get()方法。

        Events e = new HttpRequestTask().execute().get();
        

        【讨论】:

        • 错误:(93, 42) 错误:找不到符号方法HttpRequestTask()
        • 我已经更新了我的答案我从你的问题中复制了这一行,我认为你的事件类中有异步任务“HttpRequestTask”
        • 错误:(94, 78) 错误:未报告的异常InterruptedException;必须被抓住或宣布被抛出
        • 你可能会遇到更多错误,但我相信一旦你最后调用 get() 方法,你就会得到你的 Events 对象,我会再看看你的代码
        • public static class HttpRequestTask extends AsyncTask {} 在我的 Events 类中 – Stefan van de Laarschot 刚刚编辑
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-08
        • 2015-04-01
        • 2012-01-12
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多