http post blackberry (null response)

后端 未结 3 1210
终归单人心
终归单人心 2020-12-07 05:14

I have used this code mod from some url here :

    HttpConnection httpConnection = (HttpConnection) Connector.open(webservice_address,Connector.READ_WRITE);
         


        
相关标签:
3条回答
  • 2020-12-07 05:32

    Try like this sample code:

    public class ConnectionThread extends Thread 
    {       
    String url;
    HttpConnection httpConnection;
    InputStream inputStream;
    String id="0";  
    StringBuffer stringBuffer=new StringBuffer();
    public ConnectionThread(String url) 
    {   
        this.url=url;       
    }
    public void run() 
    {
        try
        {
            httpConnection=(HttpConnection)Connector.open("Giver Your URL"+";interface=wifi");
            URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
    
            oPostData.append("category",id);//Parameters list;  
            oPostData.append("categoryName","Categ1");
    
            System.out.println("================"+oPostData.toString());
            httpConnection.setRequestMethod(HttpConnection.POST);
            httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, oPostData.getContentType());
            byte [] postBytes = oPostData.getBytes();
            httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
            OutputStream strmOut = httpConnection.openOutputStream();
            strmOut.write(postBytes);
            strmOut.flush();
            strmOut.close();
    
            int response=httpConnection.getResponseCode();
            if(response==HttpConnection.HTTP_OK)
            {
                inputStream = httpConnection.openInputStream();
                int c;
                while((c=inputStream.read())!=-1)
                {
                    stringBuffer.append((char)c);
                }
                callBack(stringBuffer.toString());
    
            }
            else
            {
                callBack("ERROR");
            }
        }
        catch (Exception e) 
        {
            synchronized (UiApplication.getEventLock()) 
            {
                UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
                StartUp.exceptionHandling(e.getMessage());
            }           
        }
        finally
        {
            try 
            {
                if(httpConnection!=null)
                {
                    httpConnection.close();
                }
                if(inputStream!=null)
                {
                    inputStream.close();
                }
            } 
            catch (Exception e2) 
            {}          
        }       
    }
    private void callBack(String response)
    {
        if(response.equals("ERROR"))
        {
            UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
            // Put an alert here that "URL Not found";
        }
        else
        {
            try
            {
                System.out.println(response);
                //do what you want;                                     
            }
            catch (Exception e) 
            {
                // Put an alert here that "Data Not found";
            }
        }
    }   
    }
    

    This is a sample code for POST the data;

    0 讨论(0)
  • 2020-12-07 05:36

    Have you tried to avoid calling os.close() before reading the response?

    0 讨论(0)
  • 2020-12-07 05:39

    Cau u please add Connector.READ_WRITE in Connector.open

    HttpConnection httpConnection = (HttpConnection) Connector.open(webservice_address,Connector.READ_WRITE);
    
    0 讨论(0)
提交回复
热议问题