Forums

Finding the RSD link

  1. Hello guys.

    At the moment I am testing the wp-android client on a blog, that is not a WP blog but XMLRPC capable. The client is not able to find the rsd link to the xml-rpc interface. The reason is: The XML parser you are using to find the link is failing. I don't know if the reason is, that my blog template is HTML5 or what it is..

    Anyways: Using a XML parser for searching for RSD entries in a HTML page is "too big" in my opinion. XML parser consume *a lot* of memory, where it would be much easier to use regular expressions to find the link. RegExes are more failsafe and you don't need a parser at all, so it works for any HTML a browser can display but a XML parser don't think it's XML.

    I added these changes to AddAcount.java

    //..
    String rsdUrl = getRSDMetaTagHrefRegEx(blogURL);
    if (rsdUrl ==null) rsdUrl = getRSDMetaTagHref(blogURL);
    // ..
    private static final Pattern rsdLink = Pattern.compile("<link\\s*?rel=\"EditURI\"\\s*?type=\"application/rsd\\+xml\"\\s*?title=\"RSD\"\\s*?href=\"(.*?)\"\\s*?/>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    
    	private String getRSDMetaTagHrefRegEx(String urlString) {
    		InputStream in = getResponse(urlString);
    		if (in!=null) {
    			try {
    				String html = convertStreamToString(in);
    				Matcher matcher = rsdLink.matcher(html);
    				if (matcher.find()) {
    					String href = matcher.group(1);
    					return href;
    				}
    			} catch (IOException e) {
    				Log.e("wp_android", "IOEX", e);
    				return null;
    			}
    		}
    		return null;
    	}
    
    	private String convertStreamToString(InputStream is) throws IOException {
    		/*
    		 * To convert the InputStream to String we use the Reader.read(char[]
    		 * buffer) method. We iterate until the Reader return -1 which means
    		 * there's no more data to read. We use the StringWriter class to
    		 * produce the string.
    		 */
    		int bufSize = 8 * 1024;
    		if (is != null) {
    			Writer writer = new StringWriter();
    
    			char[] buffer = new char[bufSize];
    			try {
    				InputStreamReader ireader = new InputStreamReader(is, "UTF-8");
    				Reader reader = new BufferedReader(ireader, bufSize);
    				int n;
    				while ((n = reader.read(buffer)) != -1) {
    					writer.write(buffer, 0, n);
    				}
    				reader.close();
    				ireader.close();
    				return writer.toString();
    			} catch (OutOfMemoryError ex) {
    				Log.e("wp_android", "Convert Stream: (out of memory)");
    				writer.close();
    				writer=null;
    				System.gc();
    				return "";
    			} finally {
    				is.close();
    			}
    		} else {
    			return "";
    		}
    	}

    This works on my blog now, too.

    And btw: You are very often assuming, that "everything is there". When parsing xml-rpc responses, you are allways using value.toString(), not checking if value is null at that moment. That is effective but nasty IMHO. The client crashes easily without a useful explanation. Would be cool, if the client would be more "failsafe".

    I would happily add some code I've done and I will do, if you are interested in this stuff. :)

    Regards, Grischa

  2. Sure, patches are welcome. Can you attach a diff to a ticket at http://android.trac.wordpress.org?

  3. I have to create another account for it? One Wordpress account, one for the forum and one for the tracker?
    To create an account for the tracker I have to go to wordpress.org. Perhaps I am blind but I can't find any "register" option there. Shouldn't it be my WP acc?

  4. Yes, you should be able to log in with your .org account and create a ticket and attach the patch. If not, you can shoot the diff over to android at automattic dot com.

    Signup is: http://wordpress.org/support/register.php

Topic Closed

This topic has been closed to new replies.


About this Topic

Tags