【问题标题】:J2ME HttpConnection on motorola i290摩托罗拉 i290 上的 J2ME HttpConnection
【发布时间】:2012-03-20 02:06:46
【问题描述】:

我有以下问题。当我在模拟器中执行它时,这个 midlet 工作正常,但是当我将它上传到我的摩托罗拉 i290(与 nextel 公司)并启动它时,它会冻结。所以永远不会启动(程序开始向 php 页面发出请求)

这是代码,如果有人知道为什么我的程序在 Wireless Toolkit 仿真器中运行良好而不是在手机中运行良好,请告诉我。谢谢

这是我的“连接”方法

String getGrade(String url) throws IOException {
    HttpConnection c = null;
    InputStream is = null;
    OutputStream os = null;
    StringBuffer b = new StringBuffer();
    TextBox t = null;
    int x = 5, y =7;
    try {
      c = (HttpConnection)Connector.open(url,Connector.READ,true);
      c.setRequestMethod(HttpConnection.GET);
      c.setRequestProperty("IF-Modified-Since", "10 Nov 2000 17:29:12 GMT");
      c.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.1");
      c.setRequestProperty("Content-Language", "en-CA");
      os = c.openOutputStream();
      is = c.openDataInputStream();
      int ch;
      while ((ch = is.read()) != -1) {
        b.append((char) ch);
        System.out.println((char)ch);
      }
      t = new TextBox("Final Grades", b.toString(), 1024, 0);
    } finally {
       if(is!= null) {
          is.close();
       }
       if(os != null) {
          os.close();
       }
       if(c != null) {
          c.close();
       }
    }
    //display.setCurrent(t);
    return b.toString();
}     

【问题讨论】:

    标签: java-me httpconnection motorola


    【解决方案1】:

    我在 Sony Ericsson 遇到了同样的问题,我发现 URL 有时会在它太长时被截断。尝试使用 POST 代替

                c = (HttpConnection) Connector.open(serverURL, 3);
    
                c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");
                c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
                c.setRequestProperty("Content-Language", "en-US");
                c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                c.setRequestProperty("Content-Length", packet.getSentLength());
                c.setRequestProperty("Cache-Control", "no-store no-cache");
                c.setRequestProperty("Pragma", "no-cache");
                c.setRequestMethod(HttpConnection.POST);
                c.setRequestProperty("Content-Length", packet.getSentLength());
    
                out = c.openOutputStream();
    
                if (out != null) {
                    out.write(packet.getSentBytes());
                }
    
    
                is = c.openInputStream();
                hcCode = c.getResponseCode();
    

    这对我有用.....注意将packet更改为相关信息

    【讨论】:

    • 我不明白您所说的“数据包”是什么意思,但我尝试了您的配置并使用 POST 而不是 GET,但我仍然遇到同样的问题..
    • 它可以只是一个字符串,只需将 packet.getSentLength() 替换为字符串长度,将 packet.getSentBytes 替换为字符串字节
    • 我解决了,这是我的互联网提供商的问题,因为他们过滤了不在“私人网络”中的方向。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多