使用ObjectOuputStream通过J2ME套接字连接发送String []数组

时间:2012-12-30 13:03:17

标签: sockets java-me

我正在尝试使用String[]在j2me中发送ObjectOUputStream数组,但我一直收到此错误,

  

java.lang.IllegalArgumentException:不是HTTP URL

这是我的代码:

    OutputStream os=null;
    HttpConnection hc= null;
    ObjectOutputStream oj=null;

    //get the URL
    String serverURL=entry.getUrl();

    hc=(HttpConnection)Connector.open(serverURL, Connector.READ_WRITE, true);
    hc=(HttpConnection)Connector.open(serverURL);


    hc.setRequestMethod (HttpConnection.POST);
    hc.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
    hc.setRequestProperty ("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
    hc.setRequestProperty ("Content-Language", "en-US");

    System.out.println ("Posting to the URL: " + entry.getVectorParams());

    //open the output stream to send the post parameters
    //os=hc.openOutputStream();

    oj=(ObjectOutputStream)hc.openOutputStream();
    //writing post parameters
    String[] bg=entry.getVectorParams();

    oj.writeObject(bg);

请提出建议。

我检查了我的网址,这是正确的,关于Connector.open(),我在这里粘贴了两次,而不是在我的实际代码中。还有什么我做错了吗?

System.out.println("Posting to the URL: " + entry.getVectorParams()),这只打印帖子参数,我在这里传递了serverurl:

String serverURL=entry.getUrl();
hc=(HttpConnection)Connector.open(serverURL, Connector.READ_WRITE, true);

我的服务器网址为:http://localhost:8080/Web/gServer.jsp

1 个答案:

答案 0 :(得分:1)

serverURL变量的值不得为有效网址。尝试将其打印出来并进行检查。

你有这个调试声明:

System.out.println ("Posting to the URL: " + entry.getVectorParams());

但是打印出params而不是url。您应该打印出serverURL变量。

此外,您连续两次致电Connector.open()。没有必要这样做。

更新:我还认为您将POST参数写入连接OutputStream的方式可能存在问题。我不会使用ObjectOutputStream。有关进行J2ME POST调用的示例,请参阅something like this。基本上,您创建String POST参数,以&分隔,然后使用String.getBytes()转换为byte[]以写入OutputStream

相关问题