Use your Android phone to make a mini SMS machine

Source: Internet
Author: User

1. Android Httpserver and HTTP debugging

Android http Server:httpcore

PC http Client:httpdebug


2. Send SMS

Android comes with the Android.telephony.SmsManager pack


3. Source code:


Package Com.example.httpservertest;import Java.io.ioexception;import Java.io.interruptedioexception;import Java.net.serversocket;import Java.net.socket;import Java.util.list;import Java.util.locale;import Org.apache.http.connectionclosedexception;import Org.apache.http.httpexception;import Org.apache.http.httprequest;import Org.apache.http.httpresponse;import Org.apache.http.HttpResponseInterceptor; Import Org.apache.http.httpserverconnection;import Org.apache.http.httpstatus;import Org.apache.http.methodnotsupportedexception;import Org.apache.http.entity.stringentity;import Org.apache.http.impl.defaultconnectionreusestrategy;import org.apache.http.impl.DefaultHttpResponseFactory; Import Org.apache.http.impl.defaulthttpserverconnection;import Org.apache.http.params.basichttpparams;import Org.apache.http.params.coreconnectionpnames;import Org.apache.http.params.coreprotocolpnames;import Org.apache.http.params.httpparams;import Org.apache.http.protocol.basichttpcontext;import org.apache.htTp.protocol.httpcontext;import Org.apache.http.protocol.httpprocessor;import Org.apache.http.protocol.httprequesthandler;import Org.apache.http.protocol.httprequesthandlerregistry;import Org.apache.http.protocol.httpservice;import Org.apache.http.protocol.immutablehttpprocessor;import Org.apache.http.protocol.responseconncontrol;import Org.apache.http.protocol.responsecontent;import Org.apache.http.protocol.responsedate;import Org.apache.http.protocol.responseserver;import android.telephony.smsmanager;/** * Basic, yet fully functional and spec compliant, http/1.1 file server. * <p> * Note the purpose of this application is demonstrate the usage of * httpcore APIs. It is not a intended to demonstrate the most efficient-of-the-building an HTTP file server. * * */public class Httpserver {public static void start (string[] args) throws Exception {Thread t = new Requestlistenert Hread (8080); T.setdaemon (false); T.start ();//start the WebService Server}static class WebservicehandleR implements Httprequesthandler {public Webservicehandler () {super ();} public void handle (final HttpRequest request,final HttpResponse response, final HttpContext context) throws HttpException , IOException {String method = Request.getrequestline (). GetMethod (). toUpperCase (locale.english);//get uristring        target = Request.getrequestline (). GetURI (); Parse mobile phone number, random code string mobile = "+target.substring" (Target.indexof ("mobile") +7,target.indexof ("&"); String Regcode = target.substring (Target.indexof ("Regcode") +8); System.out.println ("mobile" + Mobile + "Regcode" + regcode);//Send smsstring content = "Csdn-lonelyrains" Your Registration Verification code is: "+ Regcode; Smsmanager Smsmanager = Smsmanager.getdefault (); list<string> texts = smsmanager.dividemessage (content); for (String text:texts) {smsmanager.sendtextmessage ( Mobile, NULL, text, NULL, NULL);} if (Method.equals ("get")) {System.out.println ("get" + target); Response.setstatuscode (HTTPSTATUS.SC_OK); stringentity entity = new Stringentity ("<xml><Method>get</method><url> "+ target +" </url></xml> "); response.setentity (entity);} else if (method.equals ("POST")) {System.out.println ("POST" + target); Response.setstatuscode (HTTPSTATUS.SC_OK); stringentity entity = new Stringentity ("<xml><method>post</method><url>" + target + "</url ></xml> "); response.setentity (entity);} Else{throw New MethodNotSupportedException (method+ "Method not Supported");}}} Static Class Requestlistenerthread extends Thread {private final serversocket serversocket;private final httpparams param S;private final Httpservice httpservice;public requestlistenerthread (int port) throws IOException {//This.serversocket = new ServerSocket (port); System.out.println ("requestlistenerthread start");//Set up the HTTP protocol processorhttpprocessor httpproc = new Immut Ablehttpprocessor (new httpresponseinterceptor[] {new Responsedate (), New Responseserver (), New Responsecontent (), New Responseconncontrol ()}); This.params = new Basichttpparams (); This.params.setIntParameter (Coreconnectionpnames.so_timeout,). Setintparameter ( coreconnectionpnames.socket_buffer_size,8 * 1024x768). Setbooleanparameter (Coreconnectionpnames.stale_connection_ CHECK, False). Setbooleanparameter (Coreconnectionpnames.tcp_nodelay, True). Setparameter (Coreprotocolpnames.origin _server, "httpcomponents/1.1");//Set up request Handlershttprequesthandlerregistry reqistry = new  Httprequesthandlerregistry (); Reqistry.register ("*", New Webservicehandler ()); Webservicehandler is used to process webservice requests. This.httpservice = new Httpservice (httpproc,new defaultconnectionreusestrategy (), New Defaulthttpresponsefactory ()); Httpservice.setparams (This.params); Httpservice.sethandlerresolver (reqistry);//Set up a registered request processor for the HTTP service. } @Overridepublic void Run () {System.out.println ("Listening on port" + This.serversocket.getLocalPort ()); System.out.println ("thread.interrupted =" + thread.interrupted ()); while (! Thread.interrupted ()) {try {//Set up HTTP connectionsocket socket = This.serversocket.accept ();D efaulthttpserverconnection conn = new defaulthttpserverconnection (); SYSTEM.OUT.PRINTLN ("Incoming connection from" + socket.getinetaddress ()); Conn.bind (socket, this.params);//Start Worker Threadthread T = new Workerthread (This.httpservice, conn); T.setdaemon (true); T.start ();} catch (Interruptedioexception ex) {break;} catch (IOException e) {System.err.println ("I/O error initialising connection t Hread: "+ e.getmessage ()); break;}}} Static Class Workerthread extends Thread {private final httpservice httpservice;private final httpserverconnection conn;p Ublic workerthread (Final httpservice httpservice,final Httpserverconnection conn) {super (); This.httpservice = Httpservice;this.conn = conn;} @Overridepublic void Run () {System.out.println ("New connection thread"); HttpContext context = new Basichttpcontext (null); try {while (! thread.interrupted () && This.conn.isOpen ()) {This.httpservice.handleRequest (this.conn, context);}} catch (Connectionclosedexception Ex) {System.err.println ("Client closed Connection"),} catch (IOException ex) {System.err.println ("I/O error:" + ex.getmes Sage ());} catch (HttpException ex) {System.err.println ("Unrecoverable HTTP protocol violation:" + ex.getmessage ());} Finally {try {this.conn.shutdown ();} catch (IOException ignore) {}}}}

4. Configuration:

Add Permissions in Androidmanifest.xml:

    <uses-permission android:name= "Android.permission.INTERNET"/>    <uses-permission android:name= " Android.permission.SEND_SMS "/>

5. Call: Call Httpserver.start (NULL) in activity to start


6. Test: Httpdebug Send test Example: http://192.168.1.101:8080/?mobile=12312341234&regcode=232423


7, the problem encountered:

1) laptop and WiFi while using the wireless network, found that the router has disabled the wireless network devices to communicate with each other function, this open AP isolation configuration needs to remove the hook:


2) A faction of the great God F1 phone call Httpcore as Httpserver, run the Times wrong: The Library ' libmaliinstr.so ' not found. Found that someone using this phone to do other also reported this mistake, change a cell phone after normal. Look at the name should be arm's Mali graphics hardware related library



Reference links

http://blog.csdn.net/jkeven/article/details/9271145

Use your Android phone to make a mini SMS machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.