zoukankan      html  css  js  c++  java
  • Android手机客户端与Servlet交换数据(by quqi99)

                                          

     转自( http://blog.csdn.net/quqi99 ) 

    手机端:

    package zhanghua.test.dataexchange;

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    public class DataExchange extends Activity {
     private EditText titleText;
     private EditText bodyText;

     @Override
     protected void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.note_edit);

      titleText = (EditText) findViewById(R.id.title);
      bodyText = (EditText) findViewById(R.id.body);

      Button confirmButton = (Button) findViewById(R.id.confirm);

      confirmButton.setOnClickListener(new View.OnClickListener() {

       public void onClick(View arg0) {
        // Bundle bundle = new Bundle();
        // bundle.putString(Notepadv3.KEY_TITLE,
        // titleText.getText().toString());
        // bundle.putString(Notepadv3.KEY_BODY,
        // bodyText.getText().toString());
        // if (rowId != null) {
        // bundle.putLong(Notepadv3.KEY_ROW_ID, rowId);
        // }
        // setResult(RESULT_OK, null, bundle);
        // finish();

        String u = "http://172.17.1.122:8083/ServerApp/ServletApp ";
        URL url = null;
        HttpURLConnection httpurlconnection = null;
        try {
         url = new URL(u);
         httpurlconnection = (HttpURLConnection) url
           .openConnection();
         httpurlconnection.setDoOutput(true);
         httpurlconnection.setRequestMethod("POST");
         // httpurlconnection.setRequestProperty("Content-Language","en-CA");
         // httpurlconnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
         // httpurlconnection.setRequestProperty("Connection","Keep-Alive");
         // //始终使用同一个HTTP连接来多次传递数据
         // 手机传给电脑的数据
         DataOutputStream dos = new DataOutputStream(
           httpurlconnection.getOutputStream());
         dos.writeUTF(titleText.getText().toString());
         dos.writeUTF(bodyText.getText().toString());
         dos.flush();
         dos.close();
         // httpurlconnection.getOutputStream().flush();
         // httpurlconnection.getOutputStream().close();
         int code = httpurlconnection.getResponseCode();
         System.out.println("code   " + code);

         // 接收servlet响应数据
         DataInputStream dis = new DataInputStream(httpurlconnection
           .getInputStream());
         String return1Str = dis.readUTF();
         String return2Str = dis.readUTF();
         System.out.println("手机接收到servlet端传来的第一个参数为:" + return1Str);
         titleText.setText(return1Str); // 显示
         // 主要起调试的作用,调试的结果将显示在WTK的控制台中
         System.out.println("手机接收到servlet端传来的第二个参数为:" + return2Str);
         bodyText.setText(return2Str); // 显示
        } catch (Exception e) {
         e.printStackTrace();
        } finally {
         if (httpurlconnection != null)
          httpurlconnection.disconnect();
        }

       }

      });
     }
    }

     

    2、电脑端

    package quqi;

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     * @date    2007/12/10
     * @author  张华
     * @descrip 模拟服务器端
     */
    public class ServletApp extends HttpServlet {
     public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws IOException, ServletException {
      response.setContentType("text/html; charset=GBK");

      // 设置响应属性
      // 接收客户端的请求
      InputStream is = request.getInputStream();
      DataInputStream dis = new DataInputStream(is);
      String tf1Str = dis.readUTF();
      String tf2Str = dis.readUTF();
      System.out.println("servlet端接收到手机传来的第一个参数为:" + tf1Str);
      // 主要起调试的作用,调试的结果显示在tomcat的启动DOS窗口中
      System.out.println("servlet端接收到手机传来的第二个参数为:" + tf2Str);
      // 对接收的参数进行处理
      String return1Str = tf1Str.concat(":早上好!");
      // 在接收到的参数后连接字符串
      String return2Str = tf2Str.concat(":晚上好!");
      // 发送处理后的参数给手机
      DataOutputStream dos = new DataOutputStream(response.getOutputStream());
      dos.writeUTF(return1Str);
      dos.writeUTF(return2Str);
      System.out.println("servlet传递给手机的第一个参数为:" + return1Str);
      System.out.println("servlet传递给手机的第二个参数为:" + return2Str);
     }

     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      doGet(request, response);
     }

     public void destroy() {
     }
     // 清除资源
    }

     

  • 相关阅读:
    动态规划:DAG-嵌套矩形
    动态规划:LCIS
    动态规划&字符串:最长公共子串
    动态规划:LCS
    动态规划:状压DP-斯坦纳树
    动态规划:数位DP
    JavaScript 正则表达式
    JavaScript 类型转换
    JavaScript typeof, null, 和 undefined
    JavaScript if...Else 语句
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318946.html
Copyright © 2011-2022 走看看