Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 231
Registered: ‎03-16-2011
My Device: 9700, 9550, and JDE
My Carrier: AIRTEL, BSNL

problem in "post data " and getting back "response data"

plz check what is the problem

 

 

 

HttpConnection hc = null;
        String response = null;
        ConnectionFactory connFact = new ConnectionFactory();
        //connFact.setTimeoutSupported(true);
        connFact.setTimeLimit(50000);
        connFact.setConnectionTimeout(50000);
        ConnectionDescriptor connDesc = null;
        try{
           
        connDesc = connFact.getConnection("http://20.20.21.112"
                + "/abc/webservices/loginauth.php");
        }catch (Exception e) {
            e.printStackTrace();
        }
        if (connDesc != null)
        {
           hc= (HttpConnection)connDesc.getConnection();
       
        }
        try {
            String uploadedData = "uid=" + "abc" + "&pwd=" + "admin123"
                    + "&masterpwd=" + "1234";

            if(hc!=null){
            hc.setRequestMethod("POST");
            hc.openOutputStream().write(uploadedData.getBytes());
            System.out.print(hc.getResponseCode()+" &&&&&&&&&&&&&&&&&&&&&&&&&&&& "+ hc.getResponseMessage());
            // hc.openInputStream();
            byte[] data;
            InputStream in = hc.openInputStream();
            data = new byte[in.available()];
            in.read(data);
            response = new String(data);
            }
        }

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (response.startsWith(("{\"auth\":\"0\"}"))) {
            return "Upload Status = 0 \n Wrong Masterpassword";
        } else if (response.startsWith("{\"auth\":\"1\"}")) {
            return "Upload Status = 1 \n Success";
        } else if (response.startsWith("{\"auth\":\"2\"}")) {
            return "Upload Status = 2 , Wrong Username or Password";
        } else
            return "Error";
    }


available on: Monday to Friday, Indian standard (day) time : )
  • Give kudos by clicking on the star icon to say "Thanks!" if you feel this response is helpful.
  • Click "Accept as Solution" if this post was what you needed. so that people who are trying to help will not waste their time reading a thread where no help is needed.

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: problem in "post data " and getting back "response data"

[ Edited ]

You seem to be missing some HTTP headers, like "Content-length" and "Accept".

 

POST requires a content length.

 

Is this in the simulator, or device?

 

Developer
Posts: 231
Registered: ‎03-16-2011
My Device: 9700, 9550, and JDE
My Carrier: AIRTEL, BSNL

Re: problem in "post data " and getting back "response data"

[ Edited ]

how to do that??? it is in simulator..................


available on: Monday to Friday, Indian standard (day) time : )
  • Give kudos by clicking on the star icon to say "Thanks!" if you feel this response is helpful.
  • Click "Accept as Solution" if this post was what you needed. so that people who are trying to help will not waste their time reading a thread where no help is needed.

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: problem in "post data " and getting back "response data"

I'm doing something along these lines and it works for me

 

hc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
		
URLEncodedPostData encoder = new URLEncodedPostData(null, false);

encoder.append("uid", "abc");
encoder.append("pwd", "admin123");
encoder.append("masterpwd", "1234");

OutputStream os = hc.openOutputStream();
os.write(encoder.getBytes());
os.flush();

response = new String(IOUtilities.streamToBytes(hc.openInputStream());

 

I'm leaving out some close() calls and whatnot, but this is the general idea. 

 

Developer
Posts: 231
Registered: ‎03-16-2011
My Device: 9700, 9550, and JDE
My Carrier: AIRTEL, BSNL

Re: problem in "post data " and getting back "response data"

[ Edited ]

not working it seem.................  response is still nullSmiley Sad


available on: Monday to Friday, Indian standard (day) time : )
  • Give kudos by clicking on the star icon to say "Thanks!" if you feel this response is helpful.
  • Click "Accept as Solution" if this post was what you needed. so that people who are trying to help will not waste their time reading a thread where no help is needed.

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: problem in "post data " and getting back "response data"

You're going to have to be more specific than just "it doesn't work." Are you getting Exceptions? Is anything coming back from the server at all? Also, are you sure that you have access to the internet on the device in the first place?

Developer
Posts: 231
Registered: ‎03-16-2011
My Device: 9700, 9550, and JDE
My Carrier: AIRTEL, BSNL

Re: problem in "post data " and getting back "response data"

conncetion is available, and seems data is posted, (however not sure) but response is not available, it will be json respnose. even responsecode is not i m getting


available on: Monday to Friday, Indian standard (day) time : )
  • Give kudos by clicking on the star icon to say "Thanks!" if you feel this response is helpful.
  • Click "Accept as Solution" if this post was what you needed. so that people who are trying to help will not waste their time reading a thread where no help is needed.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: problem in "post data " and getting back "response data"

Your code does not check the http response code from the Server.  If you don't check that you have no idea what has happened to your request.  And if it not OK, then your request has failed and you need to figure out why.  So please check it. 

Developer
Posts: 231
Registered: ‎03-16-2011
My Device: 9700, 9550, and JDE
My Carrier: AIRTEL, BSNL

Re: problem in "post data " and getting back "response data"

unable to solve even after,

 

but can u give me some snippet to post data to some site, and getback the response data..............


available on: Monday to Friday, Indian standard (day) time : )
  • Give kudos by clicking on the star icon to say "Thanks!" if you feel this response is helpful.
  • Click "Accept as Solution" if this post was what you needed. so that people who are trying to help will not waste their time reading a thread where no help is needed.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: problem in "post data " and getting back "response data"

And what is the code?

 

The HttpConnection documentation has an example of a POST. 

 

In addition, the sample source in here:

http://supportforums.blackberry.com/t5/Java-Development/Sample-HTTP-Connection-code-and-BIS-B-Access...

does as well.

 

I would recommend that you try using the sample Http code because it has more extensive debugging than the code you are using.