下面用到的 HanoHTTPD可通过上面地址下载:
1,服务现实类:
/**
* Created by zjp on 2018/6/11.
*/
这个类就是在运行的小型服务器了。serve方法用于获取客户端请求的数据,和返回请求结果。
public class HttpServer extends NanoHTTPD {
public HttpServer(String hostname,int port) {
super(hostname,port);
}
public HttpServer(int port) {
super(port);
}
@Override
public Response serve(IHTTPSession session) {
/*我在这里做了一个限制,只接受POST请求。这个是项目需求。*/
if (Method.POST.equals(session.getMethod())) {
Map files = new HashMap();
/*获取header信息,NanoHttp的header不仅仅是HTTP的header,还包括其他信息。*/
Map header = session.getHeaders();
try {
/*这句尤为重要就是将将body的数据写入files中,大家可以看看parseBody具体实现,倒现在我也不明白为啥这样写。*/
session.parseBody(files);
/*看就是这里,POST请教的body数据可以完整读出*/
String body = session.getQueryParameterString();//
CNTrace.d("header : " + header);//
CNTrace.d("body : " + body);
/*这里是从header里面获取客户端的IP地址。NanoHttpd的header包含的东西不止是HTTP heaer的内容*/
header.get("http-client-ip");
} catch (IOException e) {
e.printStackTrace();
} catch (ResponseException e) {
e.printStackTrace();
}
/*这里就是为客户端返回的信息了。我这里返回了一个200和一个HelloWorld*/
这里的返回值可以 自定义返回值可以直接去 NanoHTTPd 文件中去修改增加,下面会有介绍
return new NanoHTTPD.Response(Response.Status.OK, "text/html", "HelloWorld"); }else return new NanoHTTPD.Response(Response.Status.NOT_USE_POST, "text/html", "use post");// 这里的
//Response.Status.NOT_USE_POST 就是我自定义的。
; }}
2,自定义服务访问返回值类型。
在NanoHTTPD。java文件中找到 Status 方法,这里面就是访问返回的状态定义,如下图,最后的红线框为我自定义的部分,您也可以按这个格式添加你要的状态和返回值,中间用逗号隔开,调用时按上面提到的
Response.Status.NOT_USE_POST 即可。
3,服务启动类
/**
* Created by zjp on 2018/6/11.
*/
//服务启动相当简单,可以像下面在service中启动,也可以在activity中启动,要注意需在onDestroy方法中添加 stop方法。 下面的 8888 为设置
的端口,一般http支持的端口号都可以。
public class MainService extends Service {
private HttpServer mHttpServer = null;//这个是HttpServer的句柄。
@Override
public void onCreate() {
//在这里开启HTTP Server。
mHttpServer = new HttpServer(8888);
try {
mHttpServer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
//在这里关闭HTTP Server
if(mHttpServer != null)
mHttpServer.stop();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
下载NANOHTTPD 文件放到项目包中,这个小型服务器就OK了,你的手机就可以呗其他局域网设备访问了。
4,访问方法:
// 客户端访问方法 ip为局域网ip 端口为MainService设定DE端口
class MySender extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Log.i("ss","MySender");
try {
JSONObject jb = new JSONObject();
jb.put("我是参数名", "我是参数");
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
.add("username","admin")
.add("password","123456")
.build();
Request request = new Request.Builder()
.url("http://" + ed.getText().toString().trim() + ":8888")
.post(requestBody)
.build();
Call call = client.newCall(request);
Response response = null;
try{
response = call.execute();
if(response.isSuccessful()){
//The call was successful.print it to the log
Log.i("OKHttp",response.body().string());
}
}catch(IOException e){
e.printStackTrace();
}
String res = response.body().string();
Log.i("ss","res");
Message msg = new Message();
msg.obj = res;
handler.sendMessage(msg);
// HttpPost post = new HttpPost("http://" + ip + ":8888");
// post.setEntity(new StringEntity(request.toString()));
// HttpResponse response = new DefaultHttpClient().execute(post);
// String res = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("ss","catch");
e.printStackTrace();
}
}
}
}
在你的访问设备 调用上面的线程就可以访问 你的服务器了。重点注意1,ip为局域网下服务器设备的ip,2,端口必须和访问端设置的一样。3,传输的数据可以根据自己需要自己设定。
以上为自己开发工程的笔记,希望对你有所帮助。
请随意转载,但请注明文章来自 https://blog.csdn.net/qq_36355271/article/details/80692656
python 学习课件及代码 https://download.csdn.net/download/qq_36355271/10879594