Android (Java) 使用服务器简单发送和接收 - 快速设置挑战
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/983761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android (Java) Simple Send and receive with Server - Fast Setup Challenge
提问by gav
I'm writing an Android App and I'm looking for the fastest (In terms of setup) way for me to send data to a server and receive information back on request.
我正在编写一个 Android 应用程序,我正在寻找最快的(就设置而言)方式将数据发送到服务器并根据请求接收信息。
We're talking basic stuff. I have a log file which tells me how a user is using my application (In beta, I wouldn't runin a user experience by constantly logging usually) and I want to communicate that to my server (That I haven't setup).
我们在谈论基本的东西。我有一个日志文件,它告诉我用户如何使用我的应用程序(在测试版中,我通常不会通过不断登录来运行用户体验),并且我想将其传达给我的服务器(我尚未设置)。
I don't need security, I don't need high throughput or concurrent connections (I have 3 phones to play with) but I do need to set it up fast!
我不需要安全性,我不需要高吞吐量或并发连接(我有 3 部手机可以玩),但我确实需要快速设置!
I remember back in the day that setting up XAMPP was particularly brainless, then maybe I could use PHP to send the file from the phone to the Server?
我记得那天设置 XAMPP 特别无脑,然后我可以使用 PHP 将文件从手机发送到服务器吗?
The Server would ideally be able to respond to a GET which would allow me to send back some SQL statements which ultimately affect the UI. (It's meant to adapt the presented options depending on those most commonly used).
理想情况下,服务器能够响应 GET,这将允许我发回一些最终影响 UI 的 SQL 语句。(这意味着根据最常用的选项调整所呈现的选项)。
So there you have it, I used PHP about 4 years ago and will go down that route if it's the best but if there's some kind of new fangled port open closing binary streaming singing and dancing method that has superseeded that option I would love to know.
所以你知道了,我大约 4 年前使用过 PHP,如果它是最好的,我会沿着这条路线走下去,但是如果有某种新的开放端口关闭二进制流唱歌和跳舞的方法已经取代了该选项,我很想知道.
This tutorialseems useful but I don't really need object serialization, just text files back and forth, compressed naturally.
本教程看起来很有用,但我并不真正需要对象序列化,只需来回文本文件,自然压缩即可。
Android comes with the Apache HTTP Client 4.0built in as well as java.net.URL and java.net.HttpUrlConnection, I'd rather not add too much bult to my App with third party libraries.
Android 带有内置的Apache HTTP Client 4.0以及 java.net.URL 和 java.net.HttpUrlConnection,我不想在我的应用程序中添加太多第三方库。
Please remember that I'm setting up the server side as well so I'm looking for an overall minimum lines of code!
请记住,我也在设置服务器端,所以我正在寻找总体最少的代码行数!
采纳答案by Matt
private void sendData(ProfileVO pvo) {
Log.i(getClass().getSimpleName(), "send task - start");
HttpParams p=new BasicHttpParams();
p.setParameter("name", pvo.getName());
//Instantiate an HttpClient
HttpClient client = new DefaultHttpClient(p);
//Instantiate a GET HTTP method
try {
HttpResponse response=client.execute(new HttpGet("http://www.itortv.com/android/sendName.php"));
InputStream is=response.getEntity().getContent();
//You can convert inputstream to a string with: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(getClass().getSimpleName(), "send task - end");
}
回答by Hardwareguy
http://java.sun.com/docs/books/tutorial/networking/sockets/
http://java.sun.com/docs/books/tutorial/networking/sockets/
This is a good background tutorial to Java socket communication.
这是一个很好的 Java 套接字通信背景教程。
回答by Matt
The easiest solution for you would be to use the apache http client to get and post JSON requests to a php server.
最简单的解决方案是使用 apache http 客户端来获取 JSON 请求并将其发布到 php 服务器。
The android already has a JSON builder/parser built-in, as does PHP5, so integration is trivial, and a lot easier than using its XML/Socket counterparts.
android 已经内置了JSON 构建器/解析器,PHP5也是如此,因此集成是微不足道的,并且比使用其 XML/Socket 对应物容易得多。
If you want examples of how to do this the android side of things, here is a twitter APIthat basically communicates with twitter via their JSON rest API.
如果您想要有关如何在 android 方面执行此操作的示例,这里有一个twitter API,它基本上通过其 JSON rest API 与 twitter 进行通信。
回答by peter
Check out WWW.viewstreet.comdeveloping an Apache plugin specifically for android serverside development for Java programmers
查看WWW.viewstreet.com为 Java 程序员开发专门用于 android 服务器端开发的 Apache 插件

