編輯:關於Android編程
1、Android httpserver 和 http調試
Android http server : httpcore
PC http client : httpdebug
2、短信發送
Android自帶的android.telephony.SmsManager包
3、源碼:
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 uri String target = request.getRequestLine().getUri(); //解析手機號、隨機碼 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 sms String content = "【CSDN-lonelyrains】您的注冊驗證碼為:" + 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(" "); response.setEntity(entity); } else if (method.equals("POST") ) { System.out.println("POST " + target); response.setStatusCode(HttpStatus.SC_OK); StringEntity entity = new StringEntity(" get " + 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("RequestListenerThread start"); // Set up the HTTP protocol processor HttpProcessor 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 handlers HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry(); reqistry.register("*", new WebServiceHandler()); //WebServiceHandler用來處理webservice請求。 this.httpService = new HttpService(httpproc, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory()); httpService.setParams(this.params); httpService.setHandlerResolver(reqistry); //為http服務設置注冊好的請求處理器。 } @Override public 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 connection Socket socket = this.serversocket.accept(); DefaultHttpServerConnection conn = new DefaultHttpServerConnection(); System.out.println("Incoming connection from " + socket.getInetAddress()); conn.bind(socket, this.params); // Start worker thread Thread 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 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; } @Override public 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) { } } } } } post " + target + "
4、配置:
在AndroidManifest.xml裡添加權限:
6、測試: httpdebug 發送測試示例: http://192.168.1.101:8080/?mobile=12312341234®code=232423
7、遇到的問題:
1)筆記本和wifi同時使用無線網時,發現路由器禁用了無線網設備互相通信的功能,這個開啟AP隔離的配置需要去掉打鉤:
2)某派大神F1手機調用httpcore作為httpserver,運行時報錯:Library "libmaliinstr.so' not found。發現有人用這手機做別的也報這個錯,換一台手機後正常。看名字應該是Arm的Mali圖形硬件的相關庫
參考鏈接
http://blog.csdn.net/jkeven/article/details/9271145
先看看效果: 首先,導入包:compile files(libs/nineoldandroids-2.4.0.jar)r然後在main中創建一個widget包。 c創建V
前面照著android系統的裁剪圖片的功能自己寫了一個相似的工具。功能是大體上實現了,但留下了一個調用的問題:如何從我的程序調用這個裁剪工具,並且獲得裁剪後的圖片呢?其實
我在和ViewPager一起使用時,遇到了一些坑,按照官方給出的示例,我寫出來的代碼是這樣的布局代碼 java代碼public class MainActi
背景隨著 Material Design設計概念的提出,使得很多的開發過程中對動畫和UI的優化越來越重要,其中一個重要的動畫就是Material Deisgn : Mat