安卓怎么连接java服务器,安卓(Java)的简单发送和接收与服务器 - 快速安装挑战...

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.

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!

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?

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).

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.

This tutorial seems 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.0 built 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.

Please remember that I'm setting up the server side as well so I'm looking for an overall minimum lines of code!

Thanks,

Gav

解决方案

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");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值