Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Blackberry中实现HTTPConnection的正确方法是什么_Blackberry_Httpconnection - Fatal编程技术网

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

在Blackberry中实现HTTPConnection的正确方法是什么,blackberry,httpconnection,Blackberry,Httpconnection,我尝试将我的应用程序与远程服务器连接,并向其传递一些凭据,但我总是从服务器获得相同的响应。我尝试更改所有参数值和传递的其他请求头值,但仍然无法获得确切的解决方案。我需要告诉您,我是否使用正确的方式与服务器ping并传递值 下面是我正在使用的代码,如果我在某个地方出错,请告诉我 //HttpServiceConnection.java import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffe

我尝试将我的应用程序与远程服务器连接,并向其传递一些凭据,但我总是从服务器获得相同的响应。我尝试更改所有参数值和传递的其他请求头值,但仍然无法获得确切的解决方案。我需要告诉您,我是否使用正确的方式与服务器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;

}

}
import java.io.InputStream;
导入java.io.OutputStream;
导入java.nio.ByteBuffer;
导入javax.microedition.io.Connector;
导入javax.microedition.io.HttpConnection;
导入javax.microedition.io.HttpsConnection;
导入net.rim.device.api.system.DeviceInfo;
导入com.beacon.bb.app.util.WSMConfig;
/**
*@author N********
* 
*/
公共类HttpServiceCommunication{
公共HttpServiceCommunication(){
System.out.println(“调用Http服务通信”);
}
公共字符串sendHttpPost(字符串uri、字符串电子邮件、字符串uid、,
字符串传递)引发异常{//哈希表头
字符串响应=null;
//创建连接。。。
System.out.println(“Url”+uri);
HttpConnection _connection=null;
字符串参数=null;
if(DeviceInfo.isSimulator()){
params=“;deviceside=false”;
}否则{
params=“;deviceside=true;interface=wifi”;
}
字符串URL=uri+params;
System.out.println(“连接到Http连接”);
试一试{
_connection=(HttpConnection)Connector.open(URL);
}捕获(例外e){
e、 printStackTrace();
}
如果(_连接!=null){
_setRequestMethod(HttpConnection.POST);
System.out.println(“请求后方法”);
_connection.setRequestProperty(“用户代理”,
“配置文件/MIDP-2.0配置/CLDC-1.1”);
_connection.setRequestProperty(“内容语言”、“en-US”);
_setRequestProperty(“内容类型”、“应用程序/json”);
//设置标题(如果有)
//if(标题!=null){
//对于(枚举en=header.keys();en.hasMoreElements();){
//字符串键=(字符串)en.nextElement();
//字符串值=(字符串)头.get(键);
//_connection.setRequestProperty(键、值);
_connection.setRequestProperty(“电子邮件”,电子邮件);
//_setRequestProperty(“方法”、“登录”);
_setRequestProperty(“uid”,uid);
_setRequestProperty(“密码”,pass);
//_setRequestProperty(“uid”,uid);
// }
// }
System.out.println(“开放输出流”);
//System.out.println(“数据为”+数据);
OutputStream _OutputStream=_connection.openOutputStream();
//System.out.println(“写入数据”);
//_outputStream.write(数据);
//_outputStream.flush();//可选,getResponseCode将刷新
//获取响应代码将打开连接,发送
//请求,并读取HTTP响应头。
//标题将一直存储到请求为止。
试一试{
System.out.println(“响应代码:”+_connection.getResponseCode());
int rc=_connection.getResponseCode();
System.out.println(“响应代码:+rc”);
System.out.println(“如果HTTP正常,则响应代码:+rc+”
+(rc==HttpConnection.HTTP_OK));
if(rc==HttpConnection.HTTP\u禁止){
系统输出打印项次(“禁止”);
response=WSMConfig.NOT\u AUTH;
}else if(rc!=HttpConnection.HTTP\u OK){
响应=WSMConfig.NOT_OK;
}else if(rc==HttpConnection.HTTP\u OK){
InputStream _InputStream=_connection.openInputStream();
最终整数最大长度=128;
字节[]buf=新字节[最大长度];
int-total=0;
while(总长度<最大长度){
int count=\u inputStream.read(buf,总计,最大长度
-总数);
如果(计数<0){
打破
}
总数+=计数;
}
响应=新字符串(buf,0,总计);
//ByteBuffer bb=新的ByteBuffer(_inputStream);
//response=bb.getString();
System.out.println(“来自服务器的响应:+Response”);
//了结一切
{
如果(_inputStream!=null)
试一试{
_inputStream.close();
}捕获(例外e){
}
如果(_outputStream!=null)
试一试{
_outputStream.close();
}捕获(例外e){
}
如果(_连接!=null)
试一试{
_connection.close();
}捕获(例外e){
}
}
}
否则{
response=WSMConfig.SERVER\u错误;
}
}捕获(例外e){
e、 printStackTrace();
}
}
//System.out.println(“响应:+Response”);
返回响应;
}
}
我得到的响应类似于
{“code”:0,“err”:“缺少‘方法’”}

任何帮助都是值得感激的


谢谢

当您想向服务器传递数据时,请尝试以下方法:

//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();

然后继续执行其余的逻辑

代码看起来很好(这是来自服务器的有效JSON响应)。您是否尝试过取消注释
/\u connection.setRequestProperty(“方法”、“登录”)是的,我也尝试过这种方法,即使我很清楚必须传递给服务器的值。我也使用webclient进行了测试,但仍然得到了相同的响应。如果有人找到解决方案,请告诉我