在 Blackberry 中实现 HTTPConnection 的正确方法是什么

发布于 2024-11-05 04:12:37 字数 5141 浏览 2 评论 0原文

我厌倦了将我的应用程序与远程服务器连接并向其传递很少的凭据,但我总是从服务器得到相同的响应。我尝试更改我传递的所有参数值和其他请求标头值,但仍然无法达到确切的解决方案。我需要你,我是否使用正确的方法来 ping 服务器并传递值。

以下是我正在使用的代码,如果我在某个地方出错,请告诉我。

// HttpServiceConnection.java

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.HttpsConnection;

import net.rim.device.api.system.DeviceInfo;

import com.beacon.bb.app.util.WSMConfig;

/**
* @author N********
* 
*/
public class HttpServiceCommunication {

public HttpServiceCommunication() {
    System.out.println("Http Service Communication Called");
}

public String sendHttpPost(String uri, String email, String uid,
        String pass) throws Exception { // Hashtable header
    String response = null;
    // create the connection...
    System.out.println("Url    " + uri);
    HttpConnection _connection = null;
    String params = null;
    if (DeviceInfo.isSimulator()) {
        params = ";deviceside=false";
    } else {
        params = ";deviceside=true;interface=wifi";
    }

    String URL = uri + params;
    System.out.println("Connecting to Http Connection ");
    try {
    _connection = (HttpConnection) Connector.open(URL);
    } catch(Exception e){
        e.printStackTrace();
    }

    if (_connection != null) {

        _connection.setRequestMethod(HttpConnection.POST);
        System.out.println("After Request Method ");
        _connection.setRequestProperty("User-Agent",
                "Profile/MIDP-2.0 Configuration/CLDC-1.1");
        _connection.setRequestProperty("Content-Language", "en-US");
        _connection.setRequestProperty("Content-type", "application/json");

        // setting header if any
        // if (header != null) {
        // for (Enumeration en = header.keys(); en.hasMoreElements();) {
        // String key = (String) en.nextElement();
        // String value = (String) header.get(key);
        // _connection.setRequestProperty(key, value);

        _connection.setRequestProperty("email", email);
        //_connection.setRequestProperty("method","login");
        _connection.setRequestProperty("uid", uid);
        _connection.setRequestProperty("password", pass);

        //_connection.setRequestProperty("uid", uid);

        // }
        // }
        System.out.println("Open Output Stream  ");
        // System.out.println("Data is     "+data);
        OutputStream _outputStream = _connection.openOutputStream();
        //System.out.println("Writing data  ");
        //_outputStream.write(data);
        // _outputStream.flush(); // Optional, getResponseCode will flush

        // Getting the response code will open the connection, send the
        // request, and read the HTTP response headers.
        // The headers are stored until requested.
        try {
        System.out.println("Response Code :" + _connection.getResponseCode());
        int rc = _connection.getResponseCode();
        System.out.println("Response Code :" + rc);
        System.out.println("Response Code   :" + rc + " if HTTP OK    :"
                + (rc == HttpConnection.HTTP_OK));
        if (rc == HttpConnection.HTTP_FORBIDDEN) {
            System.out.println("FORBIDDEN");
            response = WSMConfig.NOT_AUTH;
        } else if (rc != HttpConnection.HTTP_OK) {
            response = WSMConfig.NOT_OK;
        } else if (rc == HttpConnection.HTTP_OK) {
            InputStream _inputStream = _connection.openInputStream();
            final int MAX_LENGTH = 128;
            byte[] buf = new byte[MAX_LENGTH];
            int total = 0;
            while (total < MAX_LENGTH) {
                int count = _inputStream.read(buf, total, MAX_LENGTH
                        - total);
                if (count < 0) {
                    break;
                }
                total += count;
            }
            response = new String(buf, 0, total);
            //ByteBuffer bb = new ByteBuffer(_inputStream);
            //response = bb.getString();
            System.out.println("Response from Server   :" + response);
            // close everything out
            {
                if (_inputStream != null)
                    try {
                        _inputStream.close();
                    } catch (Exception e) {
                    }
                if (_outputStream != null)
                    try {
                        _outputStream.close();
                    } catch (Exception e) {
                    }
                if (_connection != null)
                    try {
                        _connection.close();
                    } catch (Exception e) {
                    }
            }
        }
     else {
        response = WSMConfig.SERVER_ERROR;
    }
    }catch(Exception e){
        e.printStackTrace();
    }

}
    //System.out.println("Response :" + response);
    return response;

}

}

我收到类似 {"code":0,"err":"Missing 'method'."} 的响应

任何帮助都是值得赞赏的.. ..

谢谢

I tired to connect my app with the remote server and pass few credentials to it, but i am always getting a same response from the server. I tried changing all the parameter value and other request header values that i am passing, but still i can't reach the exact solution. I need to you, whether am i using the correct way to ping with the server and to pass the values.

Below is the code that i am using , Please let me know if i have gone wrong somewhere.

// HttpServiceConnection.java

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.HttpsConnection;

import net.rim.device.api.system.DeviceInfo;

import com.beacon.bb.app.util.WSMConfig;

/**
* @author N********
* 
*/
public class HttpServiceCommunication {

public HttpServiceCommunication() {
    System.out.println("Http Service Communication Called");
}

public String sendHttpPost(String uri, String email, String uid,
        String pass) throws Exception { // Hashtable header
    String response = null;
    // create the connection...
    System.out.println("Url    " + uri);
    HttpConnection _connection = null;
    String params = null;
    if (DeviceInfo.isSimulator()) {
        params = ";deviceside=false";
    } else {
        params = ";deviceside=true;interface=wifi";
    }

    String URL = uri + params;
    System.out.println("Connecting to Http Connection ");
    try {
    _connection = (HttpConnection) Connector.open(URL);
    } catch(Exception e){
        e.printStackTrace();
    }

    if (_connection != null) {

        _connection.setRequestMethod(HttpConnection.POST);
        System.out.println("After Request Method ");
        _connection.setRequestProperty("User-Agent",
                "Profile/MIDP-2.0 Configuration/CLDC-1.1");
        _connection.setRequestProperty("Content-Language", "en-US");
        _connection.setRequestProperty("Content-type", "application/json");

        // setting header if any
        // if (header != null) {
        // for (Enumeration en = header.keys(); en.hasMoreElements();) {
        // String key = (String) en.nextElement();
        // String value = (String) header.get(key);
        // _connection.setRequestProperty(key, value);

        _connection.setRequestProperty("email", email);
        //_connection.setRequestProperty("method","login");
        _connection.setRequestProperty("uid", uid);
        _connection.setRequestProperty("password", pass);

        //_connection.setRequestProperty("uid", uid);

        // }
        // }
        System.out.println("Open Output Stream  ");
        // System.out.println("Data is     "+data);
        OutputStream _outputStream = _connection.openOutputStream();
        //System.out.println("Writing data  ");
        //_outputStream.write(data);
        // _outputStream.flush(); // Optional, getResponseCode will flush

        // Getting the response code will open the connection, send the
        // request, and read the HTTP response headers.
        // The headers are stored until requested.
        try {
        System.out.println("Response Code :" + _connection.getResponseCode());
        int rc = _connection.getResponseCode();
        System.out.println("Response Code :" + rc);
        System.out.println("Response Code   :" + rc + " if HTTP OK    :"
                + (rc == HttpConnection.HTTP_OK));
        if (rc == HttpConnection.HTTP_FORBIDDEN) {
            System.out.println("FORBIDDEN");
            response = WSMConfig.NOT_AUTH;
        } else if (rc != HttpConnection.HTTP_OK) {
            response = WSMConfig.NOT_OK;
        } else if (rc == HttpConnection.HTTP_OK) {
            InputStream _inputStream = _connection.openInputStream();
            final int MAX_LENGTH = 128;
            byte[] buf = new byte[MAX_LENGTH];
            int total = 0;
            while (total < MAX_LENGTH) {
                int count = _inputStream.read(buf, total, MAX_LENGTH
                        - total);
                if (count < 0) {
                    break;
                }
                total += count;
            }
            response = new String(buf, 0, total);
            //ByteBuffer bb = new ByteBuffer(_inputStream);
            //response = bb.getString();
            System.out.println("Response from Server   :" + response);
            // close everything out
            {
                if (_inputStream != null)
                    try {
                        _inputStream.close();
                    } catch (Exception e) {
                    }
                if (_outputStream != null)
                    try {
                        _outputStream.close();
                    } catch (Exception e) {
                    }
                if (_connection != null)
                    try {
                        _connection.close();
                    } catch (Exception e) {
                    }
            }
        }
     else {
        response = WSMConfig.SERVER_ERROR;
    }
    }catch(Exception e){
        e.printStackTrace();
    }

}
    //System.out.println("Response :" + response);
    return response;

}

}

I am getting a response like {"code":0,"err":"Missing 'method'."}

Any Help is Appreciable....

Thanks

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

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

发布评论

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

评论(1

悟红尘 2024-11-12 04:12:37

当您想要将数据传递到服务器时尝试此操作:

//encode your data to send
URLEncodedPostData encoder = new URLEncodedPostData(null, false);
encoder.encode("email", email);
encoder.encode("method", "login");
encoder.encode("uid", uid);
encoder.encode("password", pass);

//Now you open up an output stream to write to the connection
OutputStream os = _connection.openOutputStream();
os.write(encoder.getBytes();
os.flush();

然后继续其余逻辑

Try this out when you're wanting to pass data to the server:

//encode your data to send
URLEncodedPostData encoder = new URLEncodedPostData(null, false);
encoder.encode("email", email);
encoder.encode("method", "login");
encoder.encode("uid", uid);
encoder.encode("password", pass);

//Now you open up an output stream to write to the connection
OutputStream os = _connection.openOutputStream();
os.write(encoder.getBytes();
os.flush();

And then continue with the rest of your logic

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