-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yanzhenjie
committed
Jun 14, 2016
0 parents
commit 230709e
Showing
36 changed files
with
1,463 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/build | ||
/.idea/ | ||
/.gradle/ | ||
/gradle/ | ||
/andserver/build/ | ||
/andserver/andserver.iml | ||
/sample/build/ | ||
/sample/sample.iml | ||
/AndServer.iml | ||
/local.properties |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# AndServer | ||
QQ交流群1:46505645 | ||
QQ交流群2:46523908 | ||
[群行为规范][0] | ||
群资源有限,请不要重复加群,谢谢。 | ||
|
||
---- | ||
##简介 | ||
AndServer是Android Http Server的简写,顾名思义AndServer是Android端搭建Http服务器的一个项目。 | ||
目的在于在Android可以很方便的搭建Http服务器,对于有为什么会在Android搭建Http服务器的同学,请移步[严振杰的CSDN博客][1]。 | ||
需要说明一下AndServer1.0.1是基于ApacheHttpCore的,因为Android弃用了ApacheHttpClient相关API,代码中会有启用的警告,这一点大家不要担心,AndServer已经处理过了,不会影响使用的。下面是个大概的介绍,看更详细请下载Demo查看。 | ||
|
||
##使用方法 | ||
* Eclipse使用Jar包,如果需要依赖源码,请自行下载。 | ||
> [下载Jar包][2] | ||
* AndroidStudio使用Gradle构建添加依赖(推荐) | ||
```groovy | ||
compile 'com.yanzhenjie:andserver:1.0.1' | ||
``` | ||
|
||
##实现AndServerRequestHandler接口,相当于Java的Servlet一样 | ||
我们每写一个服务端接口,就要一个对应的类来处理,这里要实现`AndServerRequestHandler`接口,相当于Java继承Servet一样,我们只需要处理Request,在Response中给出响应即可: | ||
```java | ||
public class AndServerTestHandler implements AndServerRequestHandler { | ||
@Override | ||
public void handle(HttpRequest rq, HttpResponse rp, HttpContext ct) throws HttpException, IOException { | ||
response.setEntity(new StringEntity("请求成功。", "utf-8")); | ||
} | ||
} | ||
``` | ||
|
||
##在AndServer上注册接口名称,并启动服务器 | ||
在启动的时候最好放在Service中,这里给出启动的关键代码。 | ||
```java | ||
AndServerBuild andServerBuild = AndServerBuild.create(); | ||
andServerBuild.setPort(4477);// 指定http端口号。 | ||
|
||
// 注册接口。 | ||
andServerBuild.add("test", new AndServerTestHandler()); | ||
// 这里还可以注册很多接口。 | ||
|
||
// 启动服务器。 | ||
AndServer andServer = andServerBuild.build(); | ||
andServer.launch(); | ||
``` | ||
到这里就完成了,相当于写好Servlet,然后注册一下就好了。 | ||
|
||
##其他设备如何访问 | ||
如果是浏览器方法,和我们普通访问网站没有区别,比如访问我们上面的接口: | ||
```html | ||
在Android本机访问的地址就是:http://locahost:4477/test。 | ||
局域网其他设置访问地址类似:http://192.168.1.116:4477/test。 | ||
``` | ||
但是我们一般都是APP直接访问的,推荐[使用NoHttp][3],NoHttp是我的另一个Http客户端的项目,和AndServer正好是相对的,一个做服务端,一个做客户端。 | ||
|
||
##停止AndServer | ||
```java | ||
if(andServer != null && andServer.isRunning()) { | ||
andServer.close(); | ||
} | ||
``` | ||
|
||
#License | ||
```text | ||
Copyright 2016 Yan Zhenjie | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
``` | ||
|
||
[0]: https://github.com/yanzhenjie/SkillGroupRule | ||
[1]: http://blog.csdn.net/yanzhenjie1003 | ||
[2]: https://github.com/yanzhenjie/AndServer/blob/master/Jar/andserver1.0.1.jar?raw=true | ||
[3]: https://github.com/yanzhenjie/NoHttp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion 23 | ||
buildToolsVersion "23.0.3" | ||
resourcePrefix "andserver_res_" | ||
|
||
defaultConfig { | ||
minSdkVersion 8 | ||
targetSdkVersion 23 | ||
versionCode 1 | ||
versionName '1.0.1' | ||
} | ||
} | ||
|
||
dependencies { | ||
compile fileTree(dir: 'libs', includes: ['*.jar']) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest package="com.yanzhenjie.andserver"> | ||
|
||
</manifest> |
50 changes: 50 additions & 0 deletions
50
andserver/src/main/java/com/yanzhenjie/andserver/AndServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright © Yan Zhenjie. All Rights Reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.yanzhenjie.andserver; | ||
|
||
/** | ||
* <p>The control of the server.</p> | ||
* Created on 2016/6/13. | ||
* | ||
* @author Yan Zhenjie. | ||
*/ | ||
public interface AndServer { | ||
|
||
/** | ||
* start. | ||
*/ | ||
void launch(); | ||
|
||
/** | ||
* stop. | ||
*/ | ||
void close(); | ||
|
||
/** | ||
* Accept is cycle。 | ||
* | ||
* @return return true, not return false. | ||
*/ | ||
boolean isLooping(); | ||
|
||
/** | ||
* Running? | ||
* | ||
* @return return true, not return false. | ||
*/ | ||
boolean isRunning(); | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
andserver/src/main/java/com/yanzhenjie/andserver/AndServerBuild.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright © Yan Zhenjie. All Rights Reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.yanzhenjie.andserver; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* <p>AndServer entrance.</p> | ||
* Created on 2016/6/13. | ||
* | ||
* @author Yan Zhenjie. | ||
*/ | ||
public class AndServerBuild { | ||
|
||
/** | ||
* Socket port. | ||
*/ | ||
private int port = 4477; | ||
/** | ||
* Timeout. | ||
*/ | ||
private int timeout = 8 * 1000; | ||
/** | ||
* Intercept list. | ||
*/ | ||
private Map<String, AndServerRequestHandler> mRequestHandlerMap; | ||
|
||
/** | ||
* Create {@link AndServerBuild}. | ||
* | ||
* @return {@link AndServerBuild}. | ||
*/ | ||
public static AndServerBuild create() { | ||
return new AndServerBuild(); | ||
} | ||
|
||
private AndServerBuild() { | ||
mRequestHandlerMap = new HashMap<String, AndServerRequestHandler>(); | ||
} | ||
|
||
/** | ||
* Add a intercept. | ||
* | ||
* @param intercept intercept name. | ||
* @param requestHandler {@link AndServerRequestHandler}. | ||
*/ | ||
public void add(String intercept, AndServerRequestHandler requestHandler) { | ||
this.mRequestHandlerMap.put(intercept, requestHandler); | ||
} | ||
|
||
/** | ||
* Set socket sort. | ||
* | ||
* @param port port. | ||
*/ | ||
public void setPort(int port) { | ||
this.port = port; | ||
} | ||
|
||
/** | ||
* Set connection timeout. | ||
* | ||
* @param timeout ms. | ||
*/ | ||
public void setTimeout(int timeout) { | ||
this.timeout = timeout; | ||
} | ||
|
||
/** | ||
* Build {@link AndServer}. | ||
* | ||
* @return {@link AndServer}. | ||
*/ | ||
public AndServer build() { | ||
return new DefaultAndServer(port, timeout, mRequestHandlerMap); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
andserver/src/main/java/com/yanzhenjie/andserver/AndServerRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright © Yan Zhenjie. All Rights Reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.yanzhenjie.andserver; | ||
|
||
import org.apache.http.HttpException; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.protocol.HttpContext; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* <p>Dealing with the client's request.</p> | ||
* Created on 2016/6/13. | ||
* | ||
* @author Yan Zhenjie. | ||
*/ | ||
public interface AndServerRequestHandler { | ||
|
||
/** | ||
* When is the client request is triggered. | ||
* | ||
* @param request {@link HttpRequest}. | ||
* @param response {@link HttpResponse}. | ||
* @param context {@link HttpContext}. | ||
* @throws HttpException may be. | ||
* @throws IOException read data. | ||
*/ | ||
void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException; | ||
|
||
} |
Oops, something went wrong.