IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

REST API JSON to SyncAdapter AbstractThreadedSyncAdapter


Sujet :

Android

  1. #1
    Membre régulier
    Homme Profil pro
    Architecte de base de données
    Inscrit en
    Février 2013
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Norvège

    Informations professionnelles :
    Activité : Architecte de base de données
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 53
    Points : 78
    Points
    78
    Par défaut REST API JSON to SyncAdapter AbstractThreadedSyncAdapter
    Bonjour,

    J'essaye de changer le passage d'un fichier API REST XML à API REST JSON qui renvoie les données au format JSON.

    J'ai trouvé l'exemple BasicSyncAdapter http://developer.android.com/trainin...ers/index.html

    Je crois que je dois passer par volley et gson mais je suis perdu...

    SyncAdapter
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    /*
     * Copyright 2013 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
     
    package com.example.android.network.sync.basicsyncadapter;
     
    import android.accounts.Account;
    import android.content.AbstractThreadedSyncAdapter;
    import android.content.ContentProviderClient;
    import android.content.ContentProviderOperation;
    import android.content.ContentResolver;
    import android.content.Context;
    import android.content.OperationApplicationException;
    import android.content.SyncResult;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.RemoteException;
    import android.util.Log;
     
    import com.example.android.network.sync.basicsyncadapter.net.FeedParser;
    import com.example.android.network.sync.basicsyncadapter.provider.FeedContract;
     
    import org.xmlpull.v1.XmlPullParserException;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
     
    /**
     * Define a sync adapter for the app.
     *
     * <p>This class is instantiated in {@link SyncService}, which also binds SyncAdapter to the system.
     * SyncAdapter should only be initialized in SyncService, never anywhere else.
     *
     * <p>The system calls onPerformSync() via an RPC call through the IBinder object supplied by
     * SyncService.
     */
    class SyncAdapter extends AbstractThreadedSyncAdapter {
        public static final String TAG = "SyncAdapter";
     
        /**
         * URL to fetch content from during a sync.
         *
         * <p>This points to the Android Developers Blog. (Side note: We highly recommend reading the
         * Android Developer Blog to stay up to date on the latest Android platform developments!)
         */
        private static final String FEED_URL = "http://android-developers.blogspot.com/atom.xml";
     
        /**
         * Network connection timeout, in milliseconds.
         */
        private static final int NET_CONNECT_TIMEOUT_MILLIS = 15000;  // 15 seconds
     
        /**
         * Network read timeout, in milliseconds.
         */
        private static final int NET_READ_TIMEOUT_MILLIS = 10000;  // 10 seconds
     
        /**
         * Content resolver, for performing database operations.
         */
        private final ContentResolver mContentResolver;
     
        /**
         * Project used when querying content provider. Returns all known fields.
         */
        private static final String[] PROJECTION = new String[] {
                FeedContract.Entry._ID,
                FeedContract.Entry.COLUMN_NAME_ENTRY_ID,
                FeedContract.Entry.COLUMN_NAME_TITLE,
                FeedContract.Entry.COLUMN_NAME_LINK,
                FeedContract.Entry.COLUMN_NAME_PUBLISHED};
     
        // Constants representing column positions from PROJECTION.
        public static final int COLUMN_ID = 0;
        public static final int COLUMN_ENTRY_ID = 1;
        public static final int COLUMN_TITLE = 2;
        public static final int COLUMN_LINK = 3;
        public static final int COLUMN_PUBLISHED = 4;
     
        /**
         * Constructor. Obtains handle to content resolver for later use.
         */
        public SyncAdapter(Context context, boolean autoInitialize) {
            super(context, autoInitialize);
            mContentResolver = context.getContentResolver();
        }
     
        /**
         * Constructor. Obtains handle to content resolver for later use.
         */
        public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
            super(context, autoInitialize, allowParallelSyncs);
            mContentResolver = context.getContentResolver();
        }
     
        /**
         * Called by the Android system in response to a request to run the sync adapter. The work
         * required to read data from the network, parse it, and store it in the content provider is
         * done here. Extending AbstractThreadedSyncAdapter ensures that all methods within SyncAdapter
         * run on a background thread. For this reason, blocking I/O and other long-running tasks can be
         * run <em>in situ</em>, and you don't have to set up a separate thread for them.
         .
         *
         * <p>This is where we actually perform any work required to perform a sync.
         * {@link AbstractThreadedSyncAdapter} guarantees that this will be called on a non-UI thread,
         * so it is safe to peform blocking I/O here.
         *
         * <p>The syncResult argument allows you to pass information back to the method that triggered
         * the sync.
         */
        @Override
        public void onPerformSync(Account account, Bundle extras, String authority,
                                  ContentProviderClient provider, SyncResult syncResult) {
            Log.i(TAG, "Beginning network synchronization");
            try {
                final URL location = new URL(FEED_URL);
                InputStream stream = null;
     
                try {
                    Log.i(TAG, "Streaming data from network: " + location);
                    stream = downloadUrl(location);
                    updateLocalFeedData(stream, syncResult);
                    // Makes sure that the InputStream is closed after the app is
                    // finished using it.
                } finally {
                    if (stream != null) {
                        stream.close();
                    }
                }
            } catch (MalformedURLException e) {
                Log.wtf(TAG, "Feed URL is malformed", e);
                syncResult.stats.numParseExceptions++;
                return;
            } catch (IOException e) {
                Log.e(TAG, "Error reading from network: " + e.toString());
                syncResult.stats.numIoExceptions++;
                return;
            } catch (XmlPullParserException e) {
                Log.e(TAG, "Error parsing feed: " + e.toString());
                syncResult.stats.numParseExceptions++;
                return;
            } catch (ParseException e) {
                Log.e(TAG, "Error parsing feed: " + e.toString());
                syncResult.stats.numParseExceptions++;
                return;
            } catch (RemoteException e) {
                Log.e(TAG, "Error updating database: " + e.toString());
                syncResult.databaseError = true;
                return;
            } catch (OperationApplicationException e) {
                Log.e(TAG, "Error updating database: " + e.toString());
                syncResult.databaseError = true;
                return;
            }
            Log.i(TAG, "Network synchronization complete");
        }
     
        /**
         * Read XML from an input stream, storing it into the content provider.
         *
         * <p>This is where incoming data is persisted, committing the results of a sync. In order to
         * minimize (expensive) disk operations, we compare incoming data with what's already in our
         * database, and compute a merge. Only changes (insert/update/delete) will result in a database
         * write.
         *
         * <p>As an additional optimization, we use a batch operation to perform all database writes at
         * once.
         *
         * <p>Merge strategy:
         * 1. Get cursor to all items in feed<br/>
         * 2. For each item, check if it's in the incoming data.<br/>
         *    a. YES: Remove from "incoming" list. Check if data has mutated, if so, perform
         *            database UPDATE.<br/>
         *    b. NO: Schedule DELETE from database.<br/>
         * (At this point, incoming database only contains missing items.)<br/>
         * 3. For any items remaining in incoming list, ADD to database.
         */
        public void updateLocalFeedData(final InputStream stream, final SyncResult syncResult)
                throws IOException, XmlPullParserException, RemoteException,
                OperationApplicationException, ParseException {
            final FeedParser feedParser = new FeedParser();
            final ContentResolver contentResolver = getContext().getContentResolver();
     
            Log.i(TAG, "Parsing stream as Atom feed");
            final List<FeedParser.Entry> entries = feedParser.parse(stream);
            Log.i(TAG, "Parsing complete. Found " + entries.size() + " entries");
     
     
            ArrayList<ContentProviderOperation> batch = new ArrayList<ContentProviderOperation>();
     
            // Build hash table of incoming entries
            HashMap<String, FeedParser.Entry> entryMap = new HashMap<String, FeedParser.Entry>();
            for (FeedParser.Entry e : entries) {
                entryMap.put(e.id, e);
            }
     
            // Get list of all items
            Log.i(TAG, "Fetching local entries for merge");
            Uri uri = FeedContract.Entry.CONTENT_URI; // Get all entries
            Cursor c = contentResolver.query(uri, PROJECTION, null, null, null);
            assert c != null;
            Log.i(TAG, "Found " + c.getCount() + " local entries. Computing merge solution...");
     
            // Find stale data
            int id;
            String entryId;
            String title;
            String link;
            long published;
            while (c.moveToNext()) {
                syncResult.stats.numEntries++;
                id = c.getInt(COLUMN_ID);
                entryId = c.getString(COLUMN_ENTRY_ID);
                title = c.getString(COLUMN_TITLE);
                link = c.getString(COLUMN_LINK);
                published = c.getLong(COLUMN_PUBLISHED);
                FeedParser.Entry match = entryMap.get(entryId);
                if (match != null) {
                    // Entry exists. Remove from entry map to prevent insert later.
                    entryMap.remove(entryId);
                    // Check to see if the entry needs to be updated
                    Uri existingUri = FeedContract.Entry.CONTENT_URI.buildUpon()
                            .appendPath(Integer.toString(id)).build();
                    if ((match.title != null && !match.title.equals(title)) ||
                            (match.link != null && !match.link.equals(link)) ||
                            (match.published != published)) {
                        // Update existing record
                        Log.i(TAG, "Scheduling update: " + existingUri);
                        batch.add(ContentProviderOperation.newUpdate(existingUri)
                                .withValue(FeedContract.Entry.COLUMN_NAME_TITLE, title)
                                .withValue(FeedContract.Entry.COLUMN_NAME_LINK, link)
                                .withValue(FeedContract.Entry.COLUMN_NAME_PUBLISHED, published)
                                .build());
                        syncResult.stats.numUpdates++;
                    } else {
                        Log.i(TAG, "No action: " + existingUri);
                    }
                } else {
                    // Entry doesn't exist. Remove it from the database.
                    Uri deleteUri = FeedContract.Entry.CONTENT_URI.buildUpon()
                            .appendPath(Integer.toString(id)).build();
                    Log.i(TAG, "Scheduling delete: " + deleteUri);
                    batch.add(ContentProviderOperation.newDelete(deleteUri).build());
                    syncResult.stats.numDeletes++;
                }
            }
            c.close();
     
            // Add new items
            for (FeedParser.Entry e : entryMap.values()) {
                Log.i(TAG, "Scheduling insert: entry_id=" + e.id);
                batch.add(ContentProviderOperation.newInsert(FeedContract.Entry.CONTENT_URI)
                        .withValue(FeedContract.Entry.COLUMN_NAME_ENTRY_ID, e.id)
                        .withValue(FeedContract.Entry.COLUMN_NAME_TITLE, e.title)
                        .withValue(FeedContract.Entry.COLUMN_NAME_LINK, e.link)
                        .withValue(FeedContract.Entry.COLUMN_NAME_PUBLISHED, e.published)
                        .build());
                syncResult.stats.numInserts++;
            }
            Log.i(TAG, "Merge solution ready. Applying batch update");
            mContentResolver.applyBatch(FeedContract.CONTENT_AUTHORITY, batch);
            mContentResolver.notifyChange(
                    FeedContract.Entry.CONTENT_URI, // URI where data was modified
                    null,                           // No local observer
                    false);                         // IMPORTANT: Do not sync to network
            // This sample doesn't support uploads, but if *your* code does, make sure you set
            // syncToNetwork=false in the line above to prevent duplicate syncs.
        }
     
        /**
         * Given a string representation of a URL, sets up a connection and gets an input stream.
         */
        private InputStream downloadUrl(final URL url) throws IOException {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(NET_READ_TIMEOUT_MILLIS /* milliseconds */);
            conn.setConnectTimeout(NET_CONNECT_TIMEOUT_MILLIS /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            return conn.getInputStream();
        }
    }

    le FeedParser

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
     
    /*
     * Copyright 2013 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
     
    package com.example.android.network.sync.basicsyncadapter.net;
     
    import android.text.format.Time;
    import android.util.Xml;
     
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.List;
     
    /**
     * This class parses generic Atom feeds.
     *
     * <p>Given an InputStream representation of a feed, it returns a List of entries,
     * where each list element represents a single entry (post) in the XML feed.
     *
     * <p>An example of an Atom feed can be found at:
     * http://en.wikipedia.org/w/index.php?title=Atom_(standard)&oldid=560239173#Example_of_an_Atom_1.0_feed
     */
    public class FeedParser {
     
        // Constants indicting XML element names that we're interested in
        private static final int TAG_ID = 1;
        private static final int TAG_TITLE = 2;
        private static final int TAG_PUBLISHED = 3;
        private static final int TAG_LINK = 4;
     
        // We don't use XML namespaces
        private static final String ns = null;
     
        /** Parse an Atom feed, returning a collection of Entry objects.
         *
         * @param in Atom feed, as a stream.
         * @return List of {@link Entry} objects.
         * @throws XmlPullParserException on error parsing feed.
         * @throws IOException on I/O error.
         */
        public List<Entry> parse(InputStream in)
                throws XmlPullParserException, IOException, ParseException {
            try {
                XmlPullParser parser = Xml.newPullParser();
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(in, null);
                parser.nextTag();
                return readFeed(parser);
            } finally {
                in.close();
            }
        }
     
        /**
         * Decode a feed attached to an XmlPullParser.
         *
         * @param parser Incoming XMl
         * @return List of {@link Entry} objects.
         * @throws XmlPullParserException on error parsing feed.
         * @throws IOException on I/O error.
         */
        private List<Entry> readFeed(XmlPullParser parser)
                throws XmlPullParserException, IOException, ParseException {
            List<Entry> entries = new ArrayList<Entry>();
     
            // Search for <feed> tags. These wrap the beginning/end of an Atom document.
            //
            // Example:
            // <?xml version="1.0" encoding="utf-8"?>
            // <feed xmlns="http://www.w3.org/2005/Atom">
            // ...
            // </feed>
            parser.require(XmlPullParser.START_TAG, ns, "feed");
            while (parser.next() != XmlPullParser.END_TAG) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                // Starts by looking for the <entry> tag. This tag repeates inside of <feed> for each
                // article in the feed.
                //
                // Example:
                // <entry>
                //   <title>Article title</title>
                //   <link rel="alternate" type="text/html" href="http://example.com/article/1234"/>
                //   <link rel="edit" href="http://example.com/admin/article/1234"/>
                //   <id>urn:uuid:218AC159-7F68-4CC6-873F-22AE6017390D</id>
                //   <published>2003-06-27T12:00:00Z</published>
                //   <updated>2003-06-28T12:00:00Z</updated>
                //   <summary>Article summary goes here.</summary>
                //   <author>
                //     <name>Rick Deckard</name>
                //     <email>deckard@example.com</email>
                //   </author>
                // </entry>
                if (name.equals("entry")) {
                    entries.add(readEntry(parser));
                } else {
                    skip(parser);
                }
            }
            return entries;
        }
     
        /**
         * Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them
         * off to their respective "read" methods for processing. Otherwise, skips the tag.
         */
        private Entry readEntry(XmlPullParser parser)
                throws XmlPullParserException, IOException, ParseException {
            parser.require(XmlPullParser.START_TAG, ns, "entry");
            String id = null;
            String title = null;
            String link = null;
            long publishedOn = 0;
     
            while (parser.next() != XmlPullParser.END_TAG) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                if (name.equals("id")){
                    // Example: <id>urn:uuid:218AC159-7F68-4CC6-873F-22AE6017390D</id>
                    id = readTag(parser, TAG_ID);
                } else if (name.equals("title")) {
                    // Example: <title>Article title</title>
                    title = readTag(parser, TAG_TITLE);
                } else if (name.equals("link")) {
                    // Example: <link rel="alternate" type="text/html" href="http://example.com/article/1234"/>
                    //
                    // Multiple link types can be included. readAlternateLink() will only return
                    // non-null when reading an "alternate"-type link. Ignore other responses.
                    String tempLink = readTag(parser, TAG_LINK);
                    if (tempLink != null) {
                        link = tempLink;
                    }
                } else if (name.equals("published")) {
                    // Example: <published>2003-06-27T12:00:00Z</published>
                    Time t = new Time();
                    t.parse3339(readTag(parser, TAG_PUBLISHED));
                    publishedOn = t.toMillis(false);
                } else {
                    skip(parser);
                }
            }
            return new Entry(id, title, link, publishedOn);
        }
     
        /**
         * Process an incoming tag and read the selected value from it.
         */
        private String readTag(XmlPullParser parser, int tagType)
                throws IOException, XmlPullParserException {
            String tag = null;
            String endTag = null;
     
            switch (tagType) {
                case TAG_ID:
                    return readBasicTag(parser, "id");
                case TAG_TITLE:
                    return readBasicTag(parser, "title");
                case TAG_PUBLISHED:
                    return readBasicTag(parser, "published");
                case TAG_LINK:
                    return readAlternateLink(parser);
                default:
                    throw new IllegalArgumentException("Unknown tag type: " + tagType);
            }
        }
     
        /**
         * Reads the body of a basic XML tag, which is guaranteed not to contain any nested elements.
         *
         * <p>You probably want to call readTag().
         *
         * @param parser Current parser object
         * @param tag XML element tag name to parse
         * @return Body of the specified tag
         * @throws IOException
         * @throws XmlPullParserException
         */
        private String readBasicTag(XmlPullParser parser, String tag)
                throws IOException, XmlPullParserException {
            parser.require(XmlPullParser.START_TAG, ns, tag);
            String result = readText(parser);
            parser.require(XmlPullParser.END_TAG, ns, tag);
            return result;
        }
     
        /**
         * Processes link tags in the feed.
         */
        private String readAlternateLink(XmlPullParser parser)
                throws IOException, XmlPullParserException {
            String link = null;
            parser.require(XmlPullParser.START_TAG, ns, "link");
            String tag = parser.getName();
            String relType = parser.getAttributeValue(null, "rel");
            if (relType.equals("alternate")) {
                link = parser.getAttributeValue(null, "href");
            }
            while (true) {
                if (parser.nextTag() == XmlPullParser.END_TAG) break;
                // Intentionally break; consumes any remaining sub-tags.
            }
            return link;
        }
     
        /**
         * For the tags title and summary, extracts their text values.
         */
        private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
            String result = null;
            if (parser.next() == XmlPullParser.TEXT) {
                result = parser.getText();
                parser.nextTag();
            }
            return result;
        }
     
        /**
         * Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e.,
         * if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it
         * finds the matching END_TAG (as indicated by the value of "depth" being 0).
         */
        private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                throw new IllegalStateException();
            }
            int depth = 1;
            while (depth != 0) {
                switch (parser.next()) {
                    case XmlPullParser.END_TAG:
                        depth--;
                        break;
                    case XmlPullParser.START_TAG:
                        depth++;
                        break;
                }
            }
        }
     
        /**
         * This class represents a single entry (post) in the XML feed.
         *
         * <p>It includes the data members "title," "link," and "summary."
         */
        public static class Entry {
            public final String id;
            public final String title;
            public final String link;
            public final long published;
     
            Entry(String id, String title, String link, long published) {
                this.id = id;
                this.title = title;
                this.link = link;
                this.published = published;
            }
        }
    }
    Quelqu'un saurait-il me guider un peu ?

    Merci d'avance pour votre aide.

  2. #2
    Membre régulier
    Homme Profil pro
    Architecte de base de données
    Inscrit en
    Février 2013
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Norvège

    Informations professionnelles :
    Activité : Architecte de base de données
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 53
    Points : 78
    Points
    78
    Par défaut La Syndication xml
    En fait je voulais récupérer les données feed en format JSON de puis une API donc je voulais changer l interface XmlPullParser par JSONParser mais j ai trouvé une solution qui renvoi les données au format xml nommée la Syndication qui peut également être utilisé pour décrire d'autres types de contenu de site Web afin que d'autres sites Web peuvent utiliser.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
     
    <?php
      header('Content-type: text/xml, charset=UTF-8');
     
    $file = "atom.xml";
     
      // Connexion sur le serveur de base de données
      $link = mysql_connect('localhost', 'user', 'pass')  or die('Could not connect: ' . mysql_error());
     
      // Sélection de la base de données 'football'
      mysql_select_db('database') or die('Could not select database');
     
      // Requête SQL limit 25
      $query = 'SELECT id,title,published,link,imag_link,text_entry,status FROM entry ORDER BY id DESC limit 10;';
     
      // Exécution et récupération du résultat
      $result = mysql_query($query) or die('Query failed: ' . mysql_error());
     
     
     
      // Affichage de l'entête du flux ATOM
      echo "<?xml version='1.0' encoding='UTF-8' ?>"
    ?>
    <feed xmlns="http://www.w3.org/2005/Atom">
     
      <title>Example Feed</title>
      <subtitle>A subtitle.</subtitle>
      <link href="http://example.org/feed/" rel="self" />
      <link href="http://example.org/" />
      <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
      <updated>2003-12-13T18:30:02Z</updated>
     
      <?php
      function xmlencode($data) {
        //, ENT_QUOTES
        $data = htmlspecialchars($data);
        return $data;
    }
     
      function check_input($value)
    {
    // Stripslashes
    if (get_magic_quotes_gpc())
      {
      $value = stripslashes($value);
      }
    // Quote if not a number
    if (!is_numeric($value))
      {
      $value = mysql_real_escape_string($value);
      }
    return $value;
    }
     
    // Parcours des résultats et création du flux ATOM
    $i = 0;
    while($row = mysql_fetch_array($result)) {
     if ($i > 0) {
     
     
     
      echo "</entry>";
     }
     
     $output_title = xmlencode($row['title']);
     $output_link = xmlencode($row['link']); 
     $output_imag_link = xmlencode($row['imag_link']);
     $output_text_entry = xmlencode($row['text_entry']);
     
      echo "<entry>";
      echo "<title>";
      echo $output_title;
      echo "</title>";
      echo "<link rel='alternate' type='text/html' href='http://example.com/article/report.php?id=".$row['id']."'/>";
      echo "<link rel='edit' href='http://example.com/article/report.php?id=".$row['id']."'/>";
      echo "<id>";
      echo $row['id'];
      echo "</id>";
      echo "<imag_link>";
      echo $output_imag_link;
      echo "</imag_link>";
      echo "<text_entry>";
      echo $output_text_entry;
      echo "</text_entry>";
      echo "<published>";
      echo date(DATE_ATOM, strtotime($row['published']));
      echo "</published>";
      $i++;
    }		
      ?>
     
      </entry>
    </feed>
    J espère que cette solution va aider d autres

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Impossibilité d'utiliser l'API JSON dans une servlet
    Par SpeedOverflow dans le forum Format d'échange (XML, JSON...)
    Réponses: 2
    Dernier message: 21/12/2011, 09h40
  2. python rest api
    Par altair8080 dans le forum Réseau/Web
    Réponses: 0
    Dernier message: 29/10/2011, 13h17
  3. Pay-Per-View REST API
    Par okoweb dans le forum Langage
    Réponses: 3
    Dernier message: 23/08/2010, 20h37
  4. API JSON Lib - Erreur java.lang.NoSuchMethod
    Par Marvelll dans le forum Format d'échange (XML, JSON...)
    Réponses: 0
    Dernier message: 29/01/2010, 11h04
  5. [Framework] API JSON
    Par ouioui2000 dans le forum Bibliothèques et frameworks
    Réponses: 3
    Dernier message: 20/03/2006, 11h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo