2011-04-14 24 views
0

我已经创建了一个android应用程序,用于在服务器返回适当的答案之前将消息发送到服务器进行解释。我真的试图展示无线连接。Android HTTP通信:

所以从Android手机我只是想发送消息,什么是适当的方式.... httppost? (我用套接字使用缓冲区/打印完成此操作)

在服务器类中,我应该使用httpget来接收消息吗?

然后消化了消息并决定了一个合适的结果,我会如何将它发送回android应用程序?再次使用httppost?

从android应用程序读取它我需要再次使用httpget?

示例真的很感激。请记住我正在使用http协议!

亲切的问候

西蒙

回答

1

我已经使用HttpClient了良好的效果。

(没有运行这个,省略了尝试/捕获,但它应该让你开始)。

// 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! 
+0

此外,您必须在您的AndroidManifest.xml中设置权限: Zsolti 2012-03-22 20:12:31