最近遇到一個(gè)需求需要在App中創(chuàng)建一個(gè)Http服務(wù)器供供瀏覽器調(diào)用,用了下開(kāi)源的微型Htpp服務(wù)器框架:NanoHttpd,項(xiàng)目地址:https://github.com/NanoHttpd/nanohttpd

直接上代碼
public class HttpServer extends NanoHTTPD {
public HttpServer(int port) {
super(port);
}
@Override
public Response serve(IHTTPSession session) {
HashMap<String, String> files = new HashMap<>();
Method method = session.getMethod();
if (Method.POST.equals(method)) {
try {
//notice:If the post with body data, it needs parses the body,or it can't get the body data;
session.parseBody(files);
}catch (IOException e) {
return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT,
"SERVER INTERNAL ERROR: IOException: " + e.getMessage());
}catch (ResponseException e) {
return newFixedLengthResponse(e.getStatus(), MIME_PLAINTEXT, e.getMessage());
}
}
final String postData = files.get("postData");
String transJson = Transmit.getInstance().getAuthoriseData(postData);
return newFixedLengthResponse(transJson);
}
網(wǎng)站名稱(chēng):如何在Android中實(shí)現(xiàn)一個(gè)簡(jiǎn)易的Http服務(wù)器-創(chuàng)新互聯(lián)
文章URL:http://www.chinadenli.net/article34/dcscse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、定制網(wǎng)站、商城網(wǎng)站、微信小程序、用戶(hù)體驗(yàn)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容