简体   繁体   中英

JSON Spring Framework Android

Good evening,

Im learning Android Developing in Android Studio im orginal C# developer and im now at the point i parse json to my android app

with the following code in my 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;
    }
}

i call this piece of code in my onCreateView like

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

The problem is i want to return the Object /Class Events

Like Events e = Events.HttpRequestTask().execute(); and than e = my filled object.

Does someone has any tips or ideas since im new to this :) ty very much

Regards, Stefan

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

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

Calling execute() will return AsyncTask itself, Pass the reponse to

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

For Standard Android way coding request. for network request use the volley google library. For parsing the JSON use GSON library. For simple example just go through below link https://stackoverflow.com/a/24433916/3187349 In android fallow the MVP architecture go through this tutorial. http://antonioleiva.com/mvp-android/

已经使用SpringFramework完成了它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM