Use your Android phone for mini text messaging

Source: Internet
Author: User

Use your Android phone for mini text messaging

1. Android httpserver and http debugging

Android http server: httpcore

PC http client: httpdebug


2. Text message sending

Android. telephony. SmsManager package


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. *

* Please note the purpose of this application is demonstrate the usage of * HttpCore APIs. it is NOT intended to demonstrate the most efficient way of * building an HTTP file server. * **/public class HttpServer {public static void start (String [] args) throws Exception {Thread t = new RequestListenerThread (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 the mobile phone number and random code String mobile = "86" + 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 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 (" Get "+ Target +" "); Response. setEntity (entity);} else if (method. equals ("POST") {System. out. println ("POST" + target); response. setStatusCode (HttpStatus. SC _ OK); StringEntity entity = new StringEntity (" Post "+ Target +" "); Response. setEntity (entity);} else {throw new MethodNotSupportedException (method + "method not supported") ;}} static class RequestListenerThread extends Thread {private final ServerSocket serversocket; private final HttpParams params; private final HttpService httpService; public RequestListenerThread (int port) throws IOException {// this. serversocket = new ServerSocket (port); System. out. println ("Reques TListenerThread start "); // Set up the HTTP protocol processorHttpProcessor httpproc = new ImmutableHttpProcessor (new HttpResponseInterceptor [] {new ResponseDate (), new ResponseServer (), new ResponseContent (), new ResponseConnControl ()}); this. params = new BasicHttpParams (); this. params. setIntParameter (CoreConnectionPNames. SO_TIMEOUT, 5000 ). setIntParameter (CoreConnectionPNames. SOCKET_BUFFER_SIZE, 8*1024 ). 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 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 (); DefaultHttpServerConnection 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 (interruptedio1_tio N ex) {break;} catch (IOException e) {System. err. println ("I/O error initialising connection thread:" + e. getMessage (); break ;}}} static class WorkerThread extends Thread {private final HttpService httpservice; private final HttpServerConnection conn; public WorkerThread (final HttpService httpservice, final HttpServerConnection conn) {super (); this. httpservice = httpservice; this. conn = conn ;}@ Overridep Ublic 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. getMessage ();} catch (HttpException ex) {System. err. println ("Unrecoverable HTTP protocol violation:" + ex. getMessage ();} finally {try {this. conn. shutdown () ;}catch (IOException ignore ){}}}}}

4. Configuration:

Add the permission in AndroidManifest. xml:

    
     
 

5. Call: Call Httpserver. start (null) in the activity to start the service.


6. Test: httpdebug sending test example: http: // 192.168.1.101: 8080 /? Mobile = 12312341234 & regcode = 232423


7. Problems:

1) when the notebook and wifi use wireless networks at the same time, it is found that the router has disabled the function of communication between wireless network devices. This requires removing the hook for enabling the AP isolation Configuration:


2) if an F1 mobile phone calls httpcore as the httpserver, the error "Library" libmaliinstr. so 'not found "is returned. This error is also reported when someone else uses this phone. It works normally after changing the phone. The name should be the relevant library of the Mali graphic hardware of Arm.



Reference

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

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.