Android Open Source - One-Button-App---Android X M L R P C Server






From Project

Back to project page One-Button-App---Android.

License

The source code is released under:

Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (...

If you think the Android project One-Button-App---Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.xmlrpc.android;
/*  www  .  ja  v a 2 s .c  o  m*/
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.net.Socket;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class XMLRPCServer extends XMLRPCCommon {

  private static final String RESPONSE =
    "HTTP/1.1 200 OK\n" +
    "Connection: close\n" +
    "Content-Type: text/xml\n" +
    "Content-Length: ";
  private static final String NEWLINES = "\n\n";
  private XMLRPCSerializer iXMLRPCSerializer;

  public XMLRPCServer() {
    iXMLRPCSerializer = new XMLRPCSerializer();
  }

  public MethodCall readMethodCall(Socket socket) throws IOException, XmlPullParserException
  {
    MethodCall methodCall = new MethodCall();
    InputStream inputStream = socket.getInputStream();

    XmlPullParser pullParser = xmlPullParserFromSocket(inputStream);
    
    pullParser.nextTag();
    pullParser.require(XmlPullParser.START_TAG, null, Tag.METHOD_CALL);
    pullParser.nextTag();
    pullParser.require(XmlPullParser.START_TAG, null, Tag.METHOD_NAME);

    methodCall.setMethodName(pullParser.nextText());

    pullParser.nextTag();
    pullParser.require(XmlPullParser.START_TAG, null, Tag.PARAMS);
    pullParser.nextTag(); // <param>
    
    do {
      //Log.d(Tag.LOG, "type=" + pullParser.getEventType() + ", tag=" + pullParser.getName());
      pullParser.require(XmlPullParser.START_TAG, null, Tag.PARAM);
      pullParser.nextTag(); // <value>

      Object param = iXMLRPCSerializer.deserialize(pullParser);
      methodCall.params.add(param); // add to return value

      pullParser.nextTag();
      pullParser.require(XmlPullParser.END_TAG, null, Tag.PARAM);
      pullParser.nextTag(); // <param> or </params>
      
    } while (!pullParser.getName().equals(Tag.PARAMS)); // </params>

    return methodCall;
  }
  
  XmlPullParser xmlPullParserFromSocket(InputStream socketInputStream) throws IOException, XmlPullParserException {
  
    String line, xmlRpcText = "";
    BufferedReader br = new BufferedReader(new InputStreamReader(socketInputStream));
    while ((line = br.readLine()) != null && line.length() > 0); // eat the HTTP POST headers
    while (br.ready())
      xmlRpcText = xmlRpcText + br.readLine();
    // Log.d(Tag.LOG, "xml received:" + xmlRpcText);
    
    InputStream inputStream = new ByteArrayInputStream(xmlRpcText.getBytes("UTF-8"));
    XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
    Reader streamReader = new InputStreamReader(inputStream);
    pullParser.setInput(streamReader);
    return pullParser;
  }
  
  public void respond(Socket socket, Object[] params) throws IOException {

    String content = methodResponse(params);
    String response = RESPONSE + (content.length()) + NEWLINES + content;
    OutputStream outputStream = socket.getOutputStream();
    outputStream.write(response.getBytes());
    outputStream.flush();
    outputStream.close();
    socket.close();
    Log.d(Tag.LOG, "response:" + response);
  }
  
  private String methodResponse(Object[] params)
  throws IllegalArgumentException, IllegalStateException, IOException {
    StringWriter bodyWriter = new StringWriter();
    serializer.setOutput(bodyWriter);
    serializer.startDocument(null, null);
    serializer.startTag(null, Tag.METHOD_RESPONSE);
    
    serializeParams(params);

    serializer.endTag(null, Tag.METHOD_RESPONSE);
    serializer.endDocument();
    
    return bodyWriter.toString();
  }
}




Java Source Code List

edu.cc.oba.Android_OBAActivity.java
edu.cc.oba.Android_OBAActivity.java
edu.cc.oba.ConnectWithPass.java
edu.cc.oba.ConnectWithPass.java
edu.cc.oba.CopyOfcreateConnection.java
edu.cc.oba.CopyOfcreateConnection.java
edu.cc.oba.ImageDB.java
edu.cc.oba.TestOBA.java
edu.cc.oba.TestOBA.java
edu.cc.oba.chooseImage.java
edu.cc.oba.chooseImage.java
edu.cc.oba.createConnection.java
edu.cc.oba.createConnection.java
edu.cc.oba.mainUITabs.java
edu.cc.oba.mainUITabs.java
edu.cc.oba.oneButtons.java
edu.cc.oba.oneButtons.java
org.apache.http.examples.client.ClientAbortMethod.java
org.apache.http.examples.client.ClientAuthentication.java
org.apache.http.examples.client.ClientChunkEncodedPost.java
org.apache.http.examples.client.ClientConnectionRelease.java
org.apache.http.examples.client.ClientCustomContext.java
org.apache.http.examples.client.ClientCustomSSL.java
org.apache.http.examples.client.ClientEvictExpiredConnections.java
org.apache.http.examples.client.ClientExecuteDirect.java
org.apache.http.examples.client.ClientExecuteProxy.java
org.apache.http.examples.client.ClientExecuteSOCKS.java
org.apache.http.examples.client.ClientFormLogin.java
org.apache.http.examples.client.ClientGZipContentCompression.java
org.apache.http.examples.client.ClientInteractiveAuthentication.java
org.apache.http.examples.client.ClientKerberosAuthentication.java
org.apache.http.examples.client.ClientMultiThreadedExecution.java
org.apache.http.examples.client.ClientPreemptiveBasicAuthentication.java
org.apache.http.examples.client.ClientPreemptiveDigestAuthentication.java
org.apache.http.examples.client.ClientProxyAuthentication.java
org.apache.http.examples.client.ClientWithResponseHandler.java
org.apache.http.examples.conn.ManagerConnectDirect.java
org.apache.http.examples.conn.ManagerConnectProxy.java
org.apache.http.examples.conn.OperatorConnectDirect.java
org.apache.http.examples.conn.OperatorConnectProxy.java
org.apache.http.examples.entity.mime.ClientMultipartFormPost.java
org.kxml2.wap.WbxmlParser.java
org.kxml2.wap.WbxmlSerializer.java
org.kxml2.wap.wml.Wml.java
org.xmlrpc.android.Base64Coder.java
org.xmlrpc.android.IXMLRPCSerializer.java
org.xmlrpc.android.MethodCall.java
org.xmlrpc.android.Tag.java
org.xmlrpc.android.XMLRPCClient.java
org.xmlrpc.android.XMLRPCCommon.java
org.xmlrpc.android.XMLRPCException.java
org.xmlrpc.android.XMLRPCFault.java
org.xmlrpc.android.XMLRPCSerializable.java
org.xmlrpc.android.XMLRPCSerializer.java
org.xmlrpc.android.XMLRPCServer.java