📄 xmlrpc_client.java
字号:
/* * Copyright (C) 2004 Mobile Blogger Development Team */package net.sourceforge.mobileblogger;import java.io.*;import java.util.*;import javax.microedition.io.*;import javax.xml.parsers.*;import org.xml.sax.*;import org.xml.sax.helpers.*;/** * A simple implementation of an XML-RPC client. */public class XMLRPC_Client { private static final String AGENT = "MobileBlogger"; Handler handler; String url; String host; String relativeURL; /** * Initialize an XML-RPC client. * @param url the url of the XML-RPC server. */ public XMLRPC_Client(String url) { this.url = url; handler = new Handler(); /* get the host name */ host = url.substring(url.indexOf(':') + 3); host = host.substring(0, host.indexOf('/')); /* get the relative url */ relativeURL = url.substring(url.indexOf(':') + 3); relativeURL = relativeURL.substring(relativeURL.indexOf('/')); } /** * Calls an XML-RPC method with the specified parameters. * @param method the XML-RPC method that will be called. * @param params the parameters of the XML-RPC method. */ public Vector call(String method, Vector params) { StringBuffer buf = new StringBuffer(); HttpConnection hc; InputStream in; InputStreamReader reader; OutputStream out; ByteArrayOutputStream byteout; OutputStreamWriter writer; String request = toXML(method, params); try { byteout = new ByteArrayOutputStream(); writer = new OutputStreamWriter(byteout); byteout.write(request.getBytes()); writer.flush(); hc = (HttpConnection)Connector.open(url, Connector.READ_WRITE); hc.setRequestMethod(HttpConnection.POST); hc.setRequestProperty("User-Agent", AGENT); hc.setRequestProperty("Content-Type", "text/xml"); hc.setRequestProperty("Content-Length", Integer.toString(request.length())); out = hc.openOutputStream(); out.write(request.getBytes()); out.flush(); writer.flush(); System.out.println(hc.getResponseMessage()); in = hc.openInputStream(); reader = new InputStreamReader(in); char ch = 0; if(reader.ready()) { ch = (char)reader.read(); buf.append(new Character(ch).toString()); } while(reader.ready()) { ch = (char)reader.read(); buf.append(new Character(ch).toString()); } parse(buf.toString()); } catch(IOException ioe) { ioe.printStackTrace(); } return handler.getParams(); } /** * Creates an XML-RPC form of a request. */ public String toXML(String method, Vector params) { String xml = "<?xml version=\"1.0\"?>\n"; xml += "<methodCall>\n"; xml += "\t<methodName>" + method + "</methodName>\n"; xml += "\t<params>\n"; for(int i = 0; i < params.size(); i++) { xml += "\t\t<param><value>"; xml += "<" + getType(params.elementAt(i).toString()) + ">"; xml += params.elementAt(i).toString(); xml += "</" + getType(params.elementAt(i).toString()) + ">"; xml += "</value></param>\n"; } xml += "\t</params>\n"; xml += "</methodCall>\n"; return xml; } public Handler getHandler() { return handler; } /** * This method needs a lot of attention. */ public String getType(String value) { if(value.compareTo("true") == 0) return "boolean"; if(value.compareTo("false") == 0) return "boolean"; return "string"; } public void parse(String xml) { InputSource source = new InputSource(new ByteArrayInputStream(xml.getBytes())); SAXParser parser = null; SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setValidating(true); try { parser = parserFactory.newSAXParser(); parser.parse(source, handler); } catch(IOException ioe) { } catch(SAXException se) { } catch(ParserConfigurationException pce) { } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -