在 BlackBerry 中访问互联网执行 POST 的不同方式

发布于 2024-11-17 01:57:45 字数 2191 浏览 6 评论 0原文

我正在制作一个应用程序来在其中执行 POST 操作。如下所示,

//private boolean x = false;
url = "http://domain.com/login";
InputStream inputStream = null;
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
if (connDesc != null)
{
    HttpConnection httpConn;
    httpConn = (HttpConnection)connDesc.getConnection();
    httpConn.setRequestMethod(HttpConnection.POST);
    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);

    encPostData.append("username",username);
    encPostData.append("password",password);

    byte[] postData = encPostData.toString().getBytes("UTF-8");
    httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length));

    httpConn.openOutputStream().write(encPostData.getBytes()); 

    try
    {
        int Response = httpConn.getResponseCode();
        System.out.println("Response Code = "+Response+httpConn.getResponseCode());
        if (Response == 200)
        {
            System.out.println("Response Code = "+Response+httpConn.getResponseCode());
            inputStream = httpConn.openInputStream();
            byte[] responseData = new byte[10000];
            int length = 0;
            StringBuffer rawResponse = new StringBuffer(10000);
            while (-1 != (length = inputStream.read(responseData)))
            {
                rawResponse.append(new String(responseData, 0, length));
            }
            replyMessage = rawResponse.toString();
            key = replyMessage.substring(12, replyMessage.length()-2);
        }
        else if(Response == 500)
        {
            Dialog.alert("User Details Incorrect");
        }
        else
        {
            System.out.println(Response);
        }
    }
    catch (IOException e)
    {
        Dialog.alert("Connection not established");
    }
}
else
{
    Dialog.alert("Connection not established");
}
return key;

我现在想知道这在哪种类型的服务下可以工作。 如果没有 gprs/3g 但有 wifi 连接,我想让我的应用程序工作.. 或者反过来也可以..

有没有办法首先识别可用的网络,然后选择一个网络进行通信。

我很感谢在这方面的任何帮助..

谢谢你,

I am making an application to perform an POST action in it . Which is shown below

//private boolean x = false;
url = "http://domain.com/login";
InputStream inputStream = null;
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
if (connDesc != null)
{
    HttpConnection httpConn;
    httpConn = (HttpConnection)connDesc.getConnection();
    httpConn.setRequestMethod(HttpConnection.POST);
    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);

    encPostData.append("username",username);
    encPostData.append("password",password);

    byte[] postData = encPostData.toString().getBytes("UTF-8");
    httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length));

    httpConn.openOutputStream().write(encPostData.getBytes()); 

    try
    {
        int Response = httpConn.getResponseCode();
        System.out.println("Response Code = "+Response+httpConn.getResponseCode());
        if (Response == 200)
        {
            System.out.println("Response Code = "+Response+httpConn.getResponseCode());
            inputStream = httpConn.openInputStream();
            byte[] responseData = new byte[10000];
            int length = 0;
            StringBuffer rawResponse = new StringBuffer(10000);
            while (-1 != (length = inputStream.read(responseData)))
            {
                rawResponse.append(new String(responseData, 0, length));
            }
            replyMessage = rawResponse.toString();
            key = replyMessage.substring(12, replyMessage.length()-2);
        }
        else if(Response == 500)
        {
            Dialog.alert("User Details Incorrect");
        }
        else
        {
            System.out.println(Response);
        }
    }
    catch (IOException e)
    {
        Dialog.alert("Connection not established");
    }
}
else
{
    Dialog.alert("Connection not established");
}
return key;

i am now wondering under which type of service would this work.
I want to make my application work if there be no gprs/ 3g but have a wifi connection ..
Or the other way around too ..

Is there anyways to identify first the networks available n later pick a network for communication.

I appreciate any help in this regard..

Thank You,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两个我 2024-11-24 01:57:45

花一些时间阅读 TransportInfo javadoc

Take some time to read TransportInfo javadoc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文