开发者

What is correct way to implement the HTTPConnection in Blackberry

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.hasM开发者_如何转开发oreElements();) {
        // 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜