Blackberry send a HTTPPost request

后端 未结 5 1644
刺人心
刺人心 2020-12-18 10:53

i am developing and app for blackberry and i need to send a Http Post Request to my server. I\'m using the simulator in order to test my app and i found this code in order t

相关标签:
5条回答
  • 2020-12-18 11:24

    That's how you add parameters, Full answer is here:

    StringBuffer postData = new StringBuffer();
    
                        httpConn = (HttpConnection) Connector.open("https://surveys2.kenexa.com/feedbacksurveyapi/login?");
                        httpConn.setRequestMethod(HttpConnection.POST);
    
                        postData.append("username="+username);
                        postData.append("&password="+pass);
                        postData.append("&projectcode="+projectid);
                        String encodedData = postData.toString();
    
                        httpConn.setRequestProperty("Content-Language", "en-US");
                        httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                        httpConn.setRequestProperty("Content-Length",(new Integer(encodedData.length())).toString());
                        byte[] postDataByte = postData.toString().getBytes("UTF-8");
    
                        OutputStream out = httpConn.openOutputStream(); 
                        out.write(postDataByte);
                        out.close();
    
                        httpConn.getResponseCode();
    
    0 讨论(0)
  • 2020-12-18 11:28

    Here is a sample code on how to send a POST request:

    HttpConnection c = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
    c.setRequestMethod(HttpConnection.POST);
    OutputStream os = c.openOutputStream();
    os.write(request.getBytes("UTF-8"));
    os.flush();
    os.close();
    InputStream is = c.openInputStream();
    

    Just make sure you use this code in a separate thread.

    0 讨论(0)
  • 2020-12-18 11:30

    Not sure about the site you posted, but I've successfully used the sample ConnectionFactory code provided on the blackberry site.

    http://supportforums.blackberry.com/t5/Java-Development/Sample-Code-Using-the-ConnectionFactory-class-in-a-BrowserField/ta-p/532860

    Just make sure not to invoke the connection on the EventThread.

    0 讨论(0)
  • 2020-12-18 11:42

    I know this question is pretty old and OP probably solved it by now, but I've just run into the same problem and managed to fix it!

    You need to append ;deviceside=true to your URL.

    So for example, your URL will change from "http://example.com/directory/submitpost.php" to "http://example.com/directory/submitpost.php;deviceside=true".

    I found this here: http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-connection/ta-p/445879

    My POST request was timing out after 3 minutes when I did not have this (See My Comment), but it works fine with this appended to the url.

    I would also recommend using ConnectionFactory. Here's some of my code:

    Network.httpPost("http://example.com/directory/submitpost.php;deviceside=true", paramNamesArray, paramValsArray)
    
    public static void httpPost(String urlStr, String[] paramName, String[] paramVal) throws Exception {
            ConnectionFactory conFactory = new ConnectionFactory();
            conFactory.setTimeLimit(1000);
            HttpConnection conn = (HttpConnection) conFactory.getConnection(urlStr).getConnection();
            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < paramName.length; i++) {
                sb.append(paramName[i]);
                sb.append("=");
                sb.append(paramVal[i]);
                sb.append("&");
            }
            byte[] postData = sb.toString().getBytes("UTF-8");
            conn.setRequestProperty("Content-Length",new Integer(postData.length).toString());
    
            OutputStream out = conn.openOutputStream();
            out.write(postData);
            //out.flush();      //Throws an Exception for some reason/Doesn't do anything anyways
            out.close();
    
            //This writes to our connection and waits for a response 
            if (conn.getResponseCode() != 200) {
                throw new Exception(conn.getResponseMessage());
            }
    }
    
    0 讨论(0)
  • 2020-12-18 11:43
    public static ResponseBean sendRequestAndReceiveResponse(String method, String absoluteURL, String bodyData, boolean readResponseBody) 
    throws IOException
    {
        ResponseBean responseBean = new ResponseBean();
        HttpConnection httpConnection = null;
    
        try
        {
    
            String formattedURL = absoluteURL + "deviceside=true;interface=wifi"; // If you are using WiFi
            //String formattedURL = absoluteURL + "deviceside=false"; // If you are using BES
            //String formattedURL = absoluteURL + "deviceside=true"; // If you are using TCP
    
            if(DeviceInfo.isSimulator()) // if you are using simulator
                formattedURL = absoluteURL;
    
            httpConnection = (HttpConnection) Connector.open(formattedURL);
    
            httpConnection.setRequestMethod(method);
    
            if (bodyData != null && bodyData.length() > 0)
            {                               
                OutputStream os = httpConnection.openOutputStream();
                os.write(bodyData.getBytes("UTF-8"));
            }           
    
            int responseCode = httpConnection.getResponseCode();
            responseBean.setResponseCode(responseCode);
    
            if (readResponseBody)
            {
                responseBean.setBodyData(readBodyData(httpConnection));
            }
        }
        catch (IOException ex)
        {                       
            System.out.println("!!!!!!!!!!!!!!! IOException in NetworkUtil::sendRequestAndReceiveResponse(): " + ex);
            throw ex;
        }
        catch(Exception ex)
        {                       
            System.out.println("!!!!!!!!!!!!!!! Exception in NetworkUtil::sendRequestAndReceiveResponse(): " + ex);
            throw new IOException(ex.toString());
        }
        finally
        {
            if (httpConnection != null)
                httpConnection.close();
        }
    
        return responseBean;
    }
    
    public static StringBuffer readBodyData(HttpConnection httpConnection) throws UnsupportedEncodingException, IOException
    {   
        if(httpConnection == null)
            return null;
    
        StringBuffer bodyData = new StringBuffer(256);                          
        InputStream inputStream = httpConnection.openDataInputStream();
    
        byte[] data = new byte[256];
        int len = 0;
        int size = 0;
    
        while ( -1 != (len = inputStream.read(data)) )
        {
            bodyData.append(new String(data, 0, len,"UTF-8"));
            size += len;
        }
    
        if (inputStream != null)
        {
            inputStream.close();            
        }
    
        return bodyData;
    }
    
    0 讨论(0)
提交回复
热议问题