Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 268|回复: 0

[J2ME学习]基于HTTP向服务器发GET请求

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-10 23:50:16 | 显示全部楼层 |阅读模式
    1.下面的Servlet程序读取手机程序发送过来的两个参数信息,一个是用户账号,另一个是密码,然后将这两个信息返回到手机程序。
       

       

       
       

       
    package com.Test; import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
       
      
       
       
         
       

       
       
      
      

    1. public class HelloWorld extends HttpServlet {
    2.     protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    3.         // Read the parameters sent from MIDlet
    4.        String acct = req.getParameter("account"), pwd = req
    5.                 .getParameter("password");
    6.         if (acct == null || pwd == null) {
    7.             res.sendError(HttpServletResponse.SC_BAD_REQUEST,
    8.                     "Unable to read parameters");
    9.             return;
    10.         }
    11.         res.setContentType("text/plain");
    12.         PrintWriter out = res.getWriter();
    13.         out.print(this.getServletInfo() + "
    14. ");
    15.         out.print("acct:" + acct + "
    16. pwd:" + pwd);
    17.         out.close();
    18.     }
    19.     protected void doPost(HttpServletRequest req, HttpServletResponse res)
    20.             throws ServletException, IOException {
    21.         doGet(req, res);
    22.     }
    23.     /*--------------------------------------------------
    24.      * Information about servlet
    25.      *-------------------------------------------------*/
    26.     public String getServletInfo() {
    27.         return "Hello,World

    28. ";
    29.     }
    30. }
    31. 2、下面的J2ME程序通过HTTP连接方式访问servlet,并发送GET的请求,然后将servlet程序的响应信息通过一个Form显示在手机屏幕上。
    32. /*
    33. * 使用Get方式访问Servlet
    34. */
    35. import javax.microedition.midlet.*;
    36. import javax.microedition.lcdui.*;
    37. import javax.microedition.io.*;
    38. import java.io.*;
    39. public class HttpGetExample extends MIDlet implements CommandListener {
    40.         private Display display;
    41.         private Form mainForm;
    42.         private Command exitCommand;
    43.         String account = "newuser";
    44.         String password = "123456";
    45.         public HttpGetExample() {
    46.                 display = Display.getDisplay(this);
    47.                 exitCommand = new Command("Exit", Command.EXIT, 1);
    48.                 mainForm = new Form("Data from servlet");
    49.                 mainForm.addCommand(exitCommand);
    50.                 mainForm.setCommandListener(this);
    51.         }
    52.        
    53.         public void startApp() {
    54.                 display.setCurrent(mainForm);
    55.                 //访问服务器servlet
    56.                 try {
    57.                         callServlet();
    58.                 } catch (Exception e) {
    59.                         System.out.println(e.toString());
    60.                 }
    61.         }
    62.         public void pauseApp() {
    63.         }
    64.         public void destroyApp(boolean unconditional) {
    65.         }
    66.         private void callServlet() throws IOException {
    67.                 HttpConnection http = null;
    68.                 InputStream iStrm = null;
    69.                 // Data is passed at the end of url for GET
    70.                 String url = "http://127.0.0.1:8080/examples/hello" + "?" + "account="
    71.                                 + account + "&" + "password=" + password;
    72.                 try {
    73.                         http = (HttpConnection) Connector.open(url);
    74.                         //使用HttpConnection.GET方式
    75.                         // 发送Get请求
    76.                         http.setRequestMethod(HttpConnection.GET);
    77.                         // 服务器响应
    78.                         if (http.getResponseCode() == HttpConnection.HTTP_OK) {
    79.                                 iStrm = http.openInputStream();
    80.                                 // 获得头信息,没有返回
    81.                                 //获得数据信息
    82.                                 int length = (int) http.getLength();
    83.                                 if (length > 0) {
    84.                                         byte servletData[] = new byte[length];
    85.                                         iStrm.read(servletData);
    86.                                         //显示返回信息
    87.                                         mainForm.append("验证通过:
    88. "
    89.                                                         + new String(servletData));
    90.                                 } else
    91.                                         mainForm.append("不能访问数据!");
    92.                         }
    93.                 } catch (Exception e) {
    94.                         mainForm.append("网络出错");
    95.                         System.out.println(e.toString());
    96.                 } finally {
    97.                         //关闭连接对象
    98.                         if (iStrm != null)
    99.                                 iStrm.close();
    100.                         if (http != null)
    101.                                 http.close();
    102.                 }
    103.         }
    104.         public void commandAction(Command c, Displayable s) {
    105.                 if (c == exitCommand) {
    106.                         destroyApp(false);
    107.                         notifyDestroyed();
    108.                 }
    109.         }
    110. }
    复制代码

       
         
         
          
          

            
          

            
          
         
       
    复制代码

      


    源码下载:http://203.93.208.26/kj/cwb/dir7/http1.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-22 18:16 , Processed in 0.386312 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表