国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > Android客户端和服务器端数据交互的第二种方法

Android客户端和服务器端数据交互的第二种方法

来源:程序员人生   发布时间:2015-01-22 09:08:43 阅读次数:3327次

        网上有很多例子来演示Android客户端和服务器端数据如何实现交互不过这些例子大多比较复杂,对初学者来讲这是不利的,现在介绍几种代码简单、逻辑清晰的交互例子,本篇博客介绍第2种:

        1、服务器端:

        代码1:添加名为“AndroidServerServlet.java”的文件

package com.ghj.packageofservlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AndroidServerServlet extends HttpServlet { private static final long serialVersionUID = 6792396567928634227L; public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/plain; charset=UTF⑻"); request.setCharacterEncoding("UTF⑻"); System.err.println(request.getParameter("clientData")); PrintWriter printWriter = response.getWriter(); printWriter.print("您好Android客户端!"); printWriter.flush(); printWriter.close(); } }

        代码2:修改名为“web.xml”的文件

<?xml version="1.0" encoding="UTF⑻"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>AndroidServerServlet</servlet-name> <servlet-class>com.ghj.packageofservlet.AndroidServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AndroidServerServlet</servlet-name> <url-pattern>/AndroidServerServlet</url-pattern> </servlet-mapping> </web-app>

        2、Android手机客户端:

        代码1:添加名为“AndroidClientActivity.java”的文件

package com.example.androidclient; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class AndroidClientActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.android_client); Button sendButton = (Button) findViewById(R.id.send_button); sendButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { new Thread(new Runnable(){ @Override public void run() { HttpPost httpRequest = new HttpPost("http://172.16.99.207:8080/AndroidServer/AndroidServerServlet"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("clientData", "您好服务器端!")); try { Message message = new Message(); Bundle bundle = new Bundle(); httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//设置要求参数项 HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpRequest);//履行要求返回响应 if(httpResponse.getStatusLine().getStatusCode() == 200){//判断是不是要求成功 bundle.putString("msg", EntityUtils.toString(httpResponse.getEntity())); }else{ bundle.putString("msg", "没有获得到Android服务器真个响应!"); } message.setData(bundle); handler.sendMessage(message); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); } }); } private Handler handler = new Handler(){ @Override public void handleMessage(Message message) { super.handleMessage(message); Bundle bundle = message.getData(); String msg = bundle.getString("msg"); Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); } }; }

        代码2:添加名为“android_client.xml”的文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/send_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_server" /> </RelativeLayout>

       【0分下载该Demo

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生