Android的HTTP通讯: [英] Android HTTP Communication:

查看:178
本文介绍了Android的HTTP通讯:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Android应用程序将消息发送到一台服务器间pretation服务器会返回一个合适的答案了。我真的想展示的无线连接。

I have created an android application for sending messages to a server for interpretation before the server returns an appropriate answer. I am really trying to demonstrate wireless connections.

因此​​,从Android手机我只是想发送的消息,这将是适当的方式.... httppost? (我用的缓冲/打印插座做到了这一点)

So from the Android phone I simply want to send the message, what would be the appropriate way.... httppost? (I have done this with sockets using buffer/print)

在服务器类我应该使用HTTPGET来收到消息?

In the server class should I use httpget to recieve the message?

然后有消化该消息,并决定相应的结果会怎样我送它回android应用?再次httppost?

Then having digested the message and decided on an appropriate result how would I send it back to the android application? httppost again?

从Android应用读取的那样,我需要再次使用HTTPGET?

From the android app to read it would I need to once again use httpget?

例子真的会AP preciated。请记住袒露我期待使用http协议!

Examples would really be appreciated. Please bare in mind I am looking to use the http protocol!

亲切的问候

西蒙

推荐答案

我已经使用的 HttpClient的

(没有跑这一点,并省略尝试/捕获量,但它应该让你开始)。

(Didn't run this and omitted try/catches but it should get you started).

// setup the client
HttpContext httpContext = new BasicHttpContext();
DefaultHttpClient httpClient = new DefaultHttpClient();

// setup the request
HttpPost post = new HttpPost("http://someurl.com/");
List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
pairs.add(new BasicNameValuePair("name" , "value"));
post.setEntity(new UrlEncodedFormEntity(pairs));

// execute the request
BasicHttpResponse response =
        (BasicHttpResponse)httpClient.execute(post, httpContext);

// do something with the response
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));

String content;
StringBuilder contentBuilder = new StringBuilder();

String line = null;
while((line = br.readLine()) != null)
    contentBuilder.append(line);

br.close();
is.close();

content = contentBuilder.toString();

// done!

这篇关于Android的HTTP通讯:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆