Example usage for android.util Xml parse

List of usage examples for android.util Xml parse

Introduction

In this page you can find the example usage for android.util Xml parse.

Prototype

public static void parse(InputStream in, Encoding encoding, ContentHandler contentHandler)
        throws IOException, SAXException 

Source Link

Document

Parses xml from the given input stream and fires events on the given SAX handler.

Usage

From source file:com.byteridge.bookcircle.utils.ResponseParser.java

public static Response parse(InputStream inputStream) throws IOException, SAXException {
    final Response response = new Response();
    RootElement root = new RootElement("GoodreadsResponse");
    response.set_Book(Book.appendSingletonListener(root, 0));
    response.set_Request(Request.appendSingletonListener(root));
    response.set_User(User.appendSingletonListener(root, 0));
    response.set_Shelves(Shelves.appendSingletonListener(root, 0));
    response.set_Reviews(Reviews.appendSingletonListener(root, 0));
    response.set_Review(Review.appendSingletonListener(root, 0));
    try {/* ww  w .j av  a 2s.co  m*/
        Xml.parse(inputStream, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

From source file:com.byteridge.bookcircle.framework.listener.ResponseParser.java

public static Response parse(InputStream inputStream) throws IOException, SAXException {
    final Response response = new Response();

    RootElement root = new RootElement("GoodreadsResponse");

    response.set_Book(Book.appendSingletonListener(root, 0));
    response.set_Request(Request.appendSingletonListener(root));
    response.set_User(User.appendSingletonListener(root, 0));
    response.set_Shelves(Shelves.appendSingletonListener(root, 0));
    response.set_Reviews(Reviews.appendSingletonListener(root, 0));

    response.set_Review(Review.appendSingletonListener(root, 0));

    try {//  ww w .  j  a va2  s . c  o  m
        Xml.parse(inputStream, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

From source file:com.onesadjam.yagrac.xml.ResponseParser.java

public static Response parse(InputStream inputStream) throws IOException, SAXException {
    final Response response = new Response();

    RootElement root = new RootElement("GoodreadsResponse");

    response.set_Book(Book.appendSingletonListener(root, 0));
    response.set_Request(Request.appendSingletonListener(root));
    response.set_User(User.appendSingletonListener(root, 0));
    response.set_Shelves(Shelves.appendSingletonListener(root, 0));
    response.set_Reviews(Reviews.appendSingletonListener(root, 0));
    response.set_Search(Search.appendSingletonListener(root, 0));
    response.set_Followers(Followers.appendSingletonListener(root, 0));
    response.set_Friends(Friends.appendSingletonListener(root, 0));
    response.set_Following(Following.appendSingletonListener(root, 0));
    response.set_Updates(Update.appendArrayListener(root, 0));
    response.set_Review(Review.appendSingletonListener(root, 0));
    response.set_Author(Author.appendSingletonListener(root, 0));
    response.set_Comments(Comments.appendSingletonListener(root, 0));
    response.set_Events(Event.appendArrayListener(root, 0));

    try {/*  w ww .j av a2  s  . co m*/
        Xml.parse(inputStream, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

From source file:uk.co.jarofgreen.cityoutdoors.API.BaseCall.java

protected void makeCall(RootElement root) {
    Element errorTag = root.getChild("error");
    errorTag.setStartElementListener(new StartElementListener() {
        public void start(Attributes attributes) {
            errorCode = attributes.getValue("code");
        }/*w ww  . j  ava2 s .co  m*/
    });
    errorTag.setEndTextElementListener(new EndTextElementListener() {
        public void end(String body) {
            errorMessage = body;
        }
    });

    try {
        httppost.setEntity(multipartEntity);
        response = httpclient.execute(httppost);
        entity = response.getEntity();
        is = entity.getContent();

        Xml.parse(is, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (SocketException e) {
        errorCode = "A1";
        errorMessage = "Can not connect to internet";
    } catch (UnknownHostException e) {
        errorCode = "A2";
        errorMessage = "Can not connect to internet";
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.tomgibara.android.veecheck.VeecheckThread.java

private VeecheckResult performRequest(VeecheckVersion version, String uri)
        throws ClientProtocolException, IOException, IllegalStateException, SAXException {
    HttpClient client = new DefaultHttpClient();
    // TODO ideally it should be possible to adjust these constants
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
    HttpGet request = new HttpGet(version.substitute(uri));
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    try {/*  w ww. j av a  2  s .com*/
        StatusLine line = response.getStatusLine();
        // TODO this is lazy, we should consider other codes here
        if (line.getStatusCode() != 200) {
            throw new IOException("Request failed: " + line.getReasonPhrase());
        }
        Header header = response.getFirstHeader(HTTP.CONTENT_TYPE);
        Encoding encoding = identityEncoding(header);
        VeecheckResult handler = new VeecheckResult(version);
        Xml.parse(entity.getContent(), encoding, handler);
        return handler;
    } finally {
        entity.consumeContent();
    }
}

From source file:gr.ndre.scuttloid.APITask.java

protected void parseResponse(HttpResponse response) throws IOException, SAXException {
    if (this.handler != null) {
        InputStream content = response.getEntity().getContent();
        Xml.parse(content, Xml.Encoding.UTF_8, this.handler);
    }//from  w  w  w. j av a 2  s  .  co m
}

From source file:vn.mbm.phimp.me.gallery3d.picasa.PicasaApi.java

public int getAlbums(AccountManager accountManager, SyncResult syncResult, UserEntry user,
        GDataParser.EntryHandler handler) {
    // Construct the query URL for user albums.
    StringBuilder builder = new StringBuilder(BASE_URL);
    builder.append("user/");
    builder.append(Uri.encode(mAuth.user));
    builder.append(BASE_QUERY_STRING);/* w w  w .j a va  2  s.  co m*/
    builder.append("&kind=album");
    try {
        // Send the request.
        synchronized (mOperation) {
            GDataClient.Operation operation = mOperation;
            operation.inOutEtag = user.albumsEtag;
            boolean retry = false;
            int numRetries = 1;
            do {
                retry = false;
                synchronized (mClient) {
                    mClient.get(builder.toString(), operation);
                }
                switch (operation.outStatus) {
                case HttpStatus.SC_OK:
                    break;
                case HttpStatus.SC_NOT_MODIFIED:
                    return RESULT_NOT_MODIFIED;
                case HttpStatus.SC_FORBIDDEN:
                case HttpStatus.SC_UNAUTHORIZED:
                    if (!retry) {
                        accountManager.invalidateAuthToken(PicasaService.ACCOUNT_TYPE, mAuth.authToken);
                        retry = true;
                    }
                    if (numRetries == 0) {
                        ++syncResult.stats.numAuthExceptions;
                    }
                default:
                    Log.i(TAG, "getAlbums uri " + builder.toString());
                    Log.e(TAG, "getAlbums: unexpected status code " + operation.outStatus + " data: "
                            + operation.outBody.toString());
                    ++syncResult.stats.numIoExceptions;
                    return RESULT_ERROR;
                }
                --numRetries;
            } while (retry && numRetries >= 0);

            // Store the new ETag for the user/albums feed.
            user.albumsEtag = operation.inOutEtag;

            // Parse the response.
            synchronized (mParser) {
                GDataParser parser = mParser;
                parser.setEntry(mAlbumInstance);
                parser.setHandler(handler);
                try {
                    Xml.parse(operation.outBody, Xml.Encoding.UTF_8, parser);
                } catch (SocketException e) {
                    Log.e(TAG, "getAlbumPhotos: " + e);
                    ++syncResult.stats.numIoExceptions;
                    e.printStackTrace();
                    return RESULT_ERROR;
                }
            }
        }
        return RESULT_OK;
    } catch (IOException e) {
        Log.e(TAG, "getAlbums: " + e);
        ++syncResult.stats.numIoExceptions;
    } catch (SAXException e) {
        Log.e(TAG, "getAlbums: " + e);
        ++syncResult.stats.numParseExceptions;
    }
    return RESULT_ERROR;
}

From source file:com.ocp.picasa.PicasaApi.java

public int getAlbums(AccountManager accountManager, SyncResult syncResult, UserEntry user,
        GDataParser.EntryHandler handler) {
    // Construct the query URL for user albums.
    StringBuilder builder = new StringBuilder(BASE_URL);
    builder.append("user/");
    builder.append(Uri.encode(mAuth.user));
    builder.append(BASE_QUERY_STRING);//from w  w w .  ja  va  2s  . c o  m
    builder.append("&kind=album");
    try {
        // Send the request.
        synchronized (mOperation) {
            GDataClient.Operation operation = mOperation;
            operation.inOutEtag = user.albumsEtag;
            boolean retry = false;
            int numRetries = 1;
            do {
                retry = false;
                synchronized (mClient) {
                    mClient.get(builder.toString(), operation);
                }
                switch (operation.outStatus) {
                case HttpStatus.SC_OK:
                    break;
                case HttpStatus.SC_NOT_MODIFIED:
                    return RESULT_NOT_MODIFIED;
                case HttpStatus.SC_FORBIDDEN:
                case HttpStatus.SC_UNAUTHORIZED:
                    if (!retry) {
                        accountManager.invalidateAuthToken(PicasaService.ACCOUNT_TYPE, mAuth.authToken);
                        retry = true;
                    }
                    if (numRetries == 0) {
                        ++syncResult.stats.numAuthExceptions;
                    }
                default:
                    Log.i(Gallery.TAG, TAG + ": " + "getAlbums uri " + builder.toString());
                    Log.e(Gallery.TAG, TAG + ": " + "getAlbums: unexpected status code " + operation.outStatus
                            + " data: " + operation.outBody.toString());
                    ++syncResult.stats.numIoExceptions;
                    return RESULT_ERROR;
                }
                --numRetries;
            } while (retry && numRetries >= 0);

            // Store the new ETag for the user/albums feed.
            user.albumsEtag = operation.inOutEtag;

            // Parse the response.
            synchronized (mParser) {
                GDataParser parser = mParser;
                parser.setEntry(mAlbumInstance);
                parser.setHandler(handler);
                try {
                    Xml.parse(operation.outBody, Xml.Encoding.UTF_8, parser);
                } catch (SocketException e) {
                    Log.e(Gallery.TAG, TAG + ": " + "getAlbumPhotos: " + e);
                    ++syncResult.stats.numIoExceptions;
                    e.printStackTrace();
                    return RESULT_ERROR;
                }
            }
        }
        return RESULT_OK;
    } catch (IOException e) {
        Log.e(Gallery.TAG, TAG + ": " + "getAlbums: " + e);
        ++syncResult.stats.numIoExceptions;
    } catch (SAXException e) {
        Log.e(Gallery.TAG, TAG + ": " + "getAlbums: " + e);
        ++syncResult.stats.numParseExceptions;
    }
    return RESULT_ERROR;
}

From source file:com.timtory.wmgallery.picasa.PicasaApi.java

public int getAlbums(AccountManager accountManager, SyncResult syncResult, UserEntry user,
        GDataParser.EntryHandler handler) {
    // Construct the query URL for user albums.
    String baseUrl = Settings.Secure.getString(mContentResolver, SETTINGS_PICASA_GDATA_BASE_URL_KEY);
    StringBuilder builder = new StringBuilder(baseUrl != null ? baseUrl : DEFAULT_BASE_URL);
    builder.append("user/");
    builder.append(Uri.encode(mAuth.user));
    builder.append(BASE_QUERY_STRING);/* ww w.  ja  va2s . c  o m*/
    builder.append("&kind=album");
    try {
        // Send the request.
        synchronized (mOperation) {
            GDataClient.Operation operation = mOperation;
            operation.inOutEtag = user.albumsEtag;
            boolean retry = false;
            int numRetries = 1;
            do {
                retry = false;
                synchronized (mClient) {
                    mClient.get(builder.toString(), operation);
                }
                switch (operation.outStatus) {
                case HttpStatus.SC_OK:
                    break;
                case HttpStatus.SC_NOT_MODIFIED:
                    return RESULT_NOT_MODIFIED;
                case HttpStatus.SC_FORBIDDEN:
                case HttpStatus.SC_UNAUTHORIZED:
                    if (!retry) {
                        accountManager.invalidateAuthToken(PicasaService.ACCOUNT_TYPE, mAuth.authToken);
                        retry = true;
                    }
                    if (numRetries == 0) {
                        ++syncResult.stats.numAuthExceptions;
                    }
                default:
                    Log.i(TAG, "getAlbums uri " + builder.toString());
                    Log.e(TAG, "getAlbums: unexpected status code " + operation.outStatus + " data: "
                            + operation.outBody.toString());
                    ++syncResult.stats.numIoExceptions;
                    return RESULT_ERROR;
                }
                --numRetries;
            } while (retry && numRetries >= 0);

            // Store the new ETag for the user/albums feed.
            user.albumsEtag = operation.inOutEtag;

            // Parse the response.
            synchronized (mParser) {
                GDataParser parser = mParser;
                parser.setEntry(mAlbumInstance);
                parser.setHandler(handler);
                try {
                    Xml.parse(operation.outBody, Xml.Encoding.UTF_8, parser);
                } catch (SocketException e) {
                    Log.e(TAG, "getAlbumPhotos: " + e);
                    ++syncResult.stats.numIoExceptions;
                    e.printStackTrace();
                    return RESULT_ERROR;
                }
            }
        }
        return RESULT_OK;
    } catch (IOException e) {
        Log.e(TAG, "getAlbums: " + e);
        ++syncResult.stats.numIoExceptions;
    } catch (SAXException e) {
        Log.e(TAG, "getAlbums: " + e);
        ++syncResult.stats.numParseExceptions;
    }
    return RESULT_ERROR;
}

From source file:org.openintents.updatechecker.UpdateChecker.java

public VeecheckResult performRequest(VeecheckVersion version, String uri)
        throws ClientProtocolException, IOException, IllegalStateException, SAXException {
    HttpClient client = new DefaultHttpClient();
    // TODO ideally it should be possible to adjust these constants
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
    HttpGet request = new HttpGet(version.substitute(uri));
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    try {// www. j  a v a 2s .co m
        StatusLine line = response.getStatusLine();
        // TODO this is lazy, we should consider other codes here
        if (line.getStatusCode() != 200)
            throw new IOException("Request failed: " + line.getReasonPhrase());
        Header header = response.getFirstHeader(HTTP.CONTENT_TYPE);
        Encoding encoding = identityEncoding(header);
        VeecheckResult handler = new VeecheckResult(version);
        Xml.parse(entity.getContent(), encoding, handler);
        return handler;
    } finally {
        entity.consumeContent();
    }
}