BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 08-28-2009, 02:38 AM   #1
Durgm
Knows Where the Search Button Is
 
Join Date: Aug 2009
Model: 9000
PIN: N/A
Carrier: T-System
Posts: 23
Default [B]HttpConnection prob[/B]

Please Login to Remove!

Hi Myself Durg

I am newbie in BlackBerry development. I want to connect my application from internet. My code is

HttpConnection code:

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
//import javax.microedition.io.*;
import java.io.OutputStream;
//import java.util.*;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class HttpCommunicationHandler
{
HttpCommunicationHandler()
{

}

/**
* This method retrieves the data from the server using HTTP POST.
*/


public String getDataFromServerThroughPost(String getUrl, String urlData)
{
HttpConnection httpConn = null;
InputStream is = null;
OutputStream out = null;
String httpUrl = getUrl;
String paramsToBeSend = urlData;
String dataRead = "";

try {
httpConn = (HttpConnection) Connector.open(httpUrl);
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpConn.setRequestProperty("Content-Length", Integer.toString(paramsToBeSend.length()));
out = httpConn.openOutputStream();
out.write(paramsToBeSend.getBytes());
if (out != null)
out.close();
if ((httpConn.getResponseCode() == HttpConnection.HTTP_OK))
{
int length = (int) httpConn.getLength();
is = httpConn.openInputStream();
if (length == -1)
{
int chunkSize = 1024;
byte[] data = new byte[chunkSize];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int dataSizeRead = 0;// size of data read from input stream.
// keeps track of total size of data read upto now.
int totalDataSizeRead = 0;
while ((dataSizeRead = is.read(data)) != -1)
{

baos.write(data, 0, dataSizeRead);
}
dataRead = new String(baos.toByteArray());
baos.close();
}

else {
byte[] data = new byte[length];
// try to read all the bytes returned from the server.
is.read(data);
// dis.readFully(data);
dataRead = new String(data);
//if (data != null)
//data = null;

}

}
else {
// UIController.showErrorAlert("Network Error",
// "Error in communicating with the server.", d);
//System.out.println("Network Error","Error in communicating with the server.");

}
}
catch (IOException e)
{
String str ="hello";

//System.out.println("Network Error","Error in communicating with the server.", "OK", null);

}

// Since only limited number of network objects can be in open state
// it is necessary to clean them up as soon as we are done with them.
// Networking done. Clean up the network objects
finally {
try
{
if (is != null)
is.close();

}
catch (IOException e)
{

}
try
{
if (httpConn != null)
httpConn.close();
}
catch (IOException e)
{
//System.out.println("Exception occured " + t.toString());
}
}
return dataRead;
}
}


and I use this class in to my loginclass. I am sending the login and password and match after that want to print the success message.


LoginScreen code: want to generate action on click login button

try{
String datafrom = "";
HttpCommunicationHandler http = new HttpCommunicationHandler();
String getUrl =" my url";
String urlData ="?requestStr=<UserInfo><UserID>ravi</UserID><Password>kant</Password><LoginDate></LoginDate><IPAddress></IPAddress></UserInfo>";
datafrom = http.getDataFromServerThroughPost(getUrl,urlData);
System.out.println("value of datafrom"+datafrom);
}
catch(Throwable t){

}
When I click on the login button my app freeze.Please help me . its very urgent. Is there any permisson to set for network connection?

Thanks
Durg
Offline  
Old 08-28-2009, 08:09 AM   #2
hrbuckley
BlackBerry Extraordinaire
 
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Default

Quote:
Originally Posted by Durgm View Post
Hi Myself Durg

I am newbie in BlackBerry development. I want to connect my application from internet. My code is

HttpConnection code:

Code:
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
//import javax.microedition.io.*;
import java.io.OutputStream;
//import java.util.*;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class HttpCommunicationHandler  
{   
       HttpCommunicationHandler() 
       {

       }
      
        /**
         * This method retrieves the data from the server using HTTP POST.
         */
        
       
        public String getDataFromServerThroughPost(String getUrl, String urlData) 
        {
            HttpConnection httpConn = null;
            InputStream is = null;
            OutputStream out = null;
            String httpUrl = getUrl;
            String paramsToBeSend = urlData;
            String dataRead = "";
          
          try {
                httpConn = (HttpConnection) Connector.open(httpUrl);
                httpConn.setRequestMethod(HttpConnection.POST);
                httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                httpConn.setRequestProperty("Content-Length", Integer.toString(paramsToBeSend.length()));
                out = httpConn.openOutputStream();
                out.write(paramsToBeSend.getBytes());
                if (out != null)
                    out.close();
                if ((httpConn.getResponseCode() == HttpConnection.HTTP_OK))
                 {
                   int length = (int) httpConn.getLength();                               
                   is = httpConn.openInputStream();
                   if (length == -1) 
                   {
                     int chunkSize = 1024;
                     byte[] data = new byte[chunkSize];
                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
                     int dataSizeRead = 0;// size of data read from input stream.
                                   // keeps track of total size of data read upto now.
                     int totalDataSizeRead = 0;
                     while ((dataSizeRead = is.read(data)) != -1) 
                     {
                        
                        baos.write(data, 0, dataSizeRead);
                     }
                     dataRead = new String(baos.toByteArray());
                     baos.close();
                  } 
                          
                      else {                     
                           byte[] data = new byte[length];
                           // try to read all the bytes returned from the server.
                           is.read(data);
                           // dis.readFully(data);
                           dataRead = new String(data);
                           //if (data != null)
                           //data = null;

                          }
                              
                    } 
                    else {
                          // UIController.showErrorAlert("Network Error",
                          // "Error in communicating with the server.", d);
                          //System.out.println("Network Error","Error in communicating with the server.");
                                                            
                        }
              } 
              catch (IOException e) 
              {
                String str ="hello";
                        
                   //System.out.println("Network Error","Error in communicating with the server.", "OK", null);
                       
             }

             // Since only limited number of network objects can be in open state
           // it is necessary to clean them up as soon as we are done with them.
          // Networking done. Clean up the network objects
          finally {
                      try 
                      {
                          if (is != null)
                          is.close();

                      } 
                      catch (IOException e) 
                      {
                                
                      }
                      try 
                      {
                          if (httpConn != null)
                             httpConn.close();
                      } 
                      catch (IOException e) 
                      {
                             //System.out.println("Exception occured " + t.toString());
                       }
                }
              return dataRead;
       }
}

and I use this class in to my loginclass. I am sending the login and password and match after that want to print the success message.


LoginScreen code: want to generate action on click login button

Code:
 try{
                String datafrom = "";
                HttpCommunicationHandler  http = new HttpCommunicationHandler();
                String getUrl =" my url";
                String urlData ="?requestStr=<UserInfo><UserID>ravi</UserID><Password>kant</Password><LoginDate></LoginDate><IPAddress></IPAddress></UserInfo>";
                datafrom = http.getDataFromServerThroughPost(getUrl,urlData);
                System.out.println("value of datafrom"+datafrom);
            }
            catch(Throwable t){
                
                }
When I click on the login button my app freeze.Please help me . its very urgent. Is there any permisson to set for network connection?

Thanks
Durg
This belongs in the developer forum. Please read the sticky threads there.

HTTP connection activity, particularly reading from the InputStream must be done on a separate thread. If you try to do it on the event thread, when the read blocks the whole device freezes as you have noted.
__________________
My other Blackberry is a PlayBook.
Offline  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


Dec/Adac Corp. D4-10390 Rev 6 Model 1664ATTL Output Pulse Board 1983 (B16) picture

Dec/Adac Corp. D4-10390 Rev 6 Model 1664ATTL Output Pulse Board 1983 (B16)

$189.99



Niedermaier vintage Mannequins Visual Display Props Dec. 1991 Catalogue picture

Niedermaier vintage Mannequins Visual Display Props Dec. 1991 Catalogue

$141.00



VINTAGE Bruker Board DEC W4P3042 for SpectroSpin 250 picture

VINTAGE Bruker Board DEC W4P3042 for SpectroSpin 250

$79.99



Adec Vintage Adjusting Dentist Doctor Medical Stool Rolling Swivel Chair picture

Adec Vintage Adjusting Dentist Doctor Medical Stool Rolling Swivel Chair

$199.99



HSS 12pcs Valve Seat & Face Cutter 45dec 30dec 20dec for Vintage Car, Bikes ,Jee picture

HSS 12pcs Valve Seat & Face Cutter 45dec 30dec 20dec for Vintage Car, Bikes ,Jee

$235.94



Onan Electric Generating Plants Manual And Parts Catalog DEC Series 1963 Vintage picture

Onan Electric Generating Plants Manual And Parts Catalog DEC Series 1963 Vintage

$13.99







Copyright © 2004-2016 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.