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
New Contributor
Posts: 8
Registered: ‎02-12-2013
My Device: all
My Carrier: ..

http connection

hello everyone ,

i am a beginner in the bb programming, and i am reading an ebook to learn everything, the name of the ebook is "Beginning BlackBerry Development", so in tis book i found a tutoriel about making an http request i follow the instruction and i am receiving always this error:

" Request failed. Reason:javax.microedition.io.connectionNotFoundExceptionSmiley Tonguerotocol not found:net.rim.device.cldc.io.http.Protocol"

 

After that i tried to fix the problem i will send u the code but also i am receiving now this error:

"Request failed.Reason:javax.io.InterruptedIOException.local connection timed out after ~120000"

 

the code is:

package

com.beginningblackberry.networking;

 

import

net.rim.device.api.ui.UiApplication;

 

 

public

class NetworkingApplication extends UiApplication {

 

public NetworkingApplication() {

 

NetworkingMainScreen src=

 

new NetworkingMainScreen();

pushScreen(src);

}

/**

*

@paramargs

*/

publicstaticvoidmain(String[] args) {

 

// TODO Auto-generated method stub

NetworkingApplication application = new NetworkingApplication();

application.enterEventDispatcher();

 

}

 

}

..................................................................................................................................................................................

 

package

com.beginningblackberry.networking;

 

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.microedition.io.Connector;

import javax.microedition.io.HttpConnection;

 

import net.rim.device.api.ui.UiApplication;

 

public class HttpRequestDispatcher extendsThread {

 

private String url;

 

private String method; // GET or POSTprivateNetworkingMainScreen screen;

 

privatebyte[] postData;

 

public HttpRequestDispatcher(String url, String method, NetworkingMainScreen screen, byte[] postData)

{

this.url = url;

 

this.method = method;

 

this.screen = screen;

 

this.postData = postData;

}

publicvoidrun() {

 

try{

 

//set up the connection parameters//HttpConnection connection = (HttpConnection)Connector.open(url);

String url ="http://10.0.0.1;deviceside=false";

HttpConnection connection = (HttpConnection)Connector.open(url);

connection.setRequestMethod(method);

//HttpConnection doesn’t actually start a connection until you ask for some data that it needs to request

//from the server.

intresponseCode = connection.getResponseCode();

 

if (method.equals("POST") && postData != null) {

connection.setRequestProperty(

"Content-type", "application/xwww-form-urlencoded");

OutputStream requestOutput = connection.openOutputStream();

requestOutput.write(postData);

requestOutput.close();

}

//the standard HTTP response code value: 200 if the request//succeeded and some other value if it didn’t

//(it’s slightly more complicated, but for our//purposes, that’s fine). Test the response code and if it’s not

//200

//(HttpConnection.HTTP_OK), notify the main screen and stop

if(responseCode != HttpConnection.HTTP_OK) {

screen.requestFailed("Unexpected response code: "+ responseCode);

connection.close();

return;

}

 

String contentType = connection.getHeaderField("Content-type");

 

//open the connection’s input stream and read the data into a buffer.

ByteArrayOutputStream baos =

newByteArrayOutputStream();

InputStream responseData = connection.openInputStream();

byte[] buffer = newbyte[10000];

 

intbytesRead = responseData.read(buffer);

 

while(bytesRead > 0) {

baos.write(buffer, 0, bytesRead);

bytesRead = responseData.read(buffer);

}

 

baos.close();

connection.close();

screen.requestSucceeded(baos.toByteArray(), contentType);

 

 

}

catch(IOException ex) {

screen.requestFailed(ex.toString());

}

}

}

.................................................................................................................................................................................................

package com.beginningblackberry.networking;

import net.rim.blackberry.api.browser.URLEncodedPostData;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;


public class NetworkingMainScreen extends MainScreen {
 
 private EditField urlField;
 private BitmapField imageOutputField;
 private RichTextField textOutputField;
 private EditField postDataField;
 
 public NetworkingMainScreen() {
  
  setTitle("Networking");
  urlField = new EditField("URL:", "");
  textOutputField = new RichTextField();
  imageOutputField = new BitmapField();
  
  add(urlField);
  add(new SeparatorField());
  postDataField = new EditField("Post data:", "");
  add(postDataField);
  add(new SeparatorField());
  add(new LabelField("Image retrieved:"));
  add(imageOutputField);
  add(new SeparatorField());
  add(new LabelField("text retrieved:"));
  add(textOutputField);
 }
 
 
 //add a menu item and skeleton method to initiate the HTTP request to NetworkingMainScreen
 private void postURL() {
  String postString = postDataField.getText();
  URLEncodedPostData encodedData = new URLEncodedPostData(null, false);
  encodedData.append("content", postString);
  HttpRequestDispatcher dispatcher = new HttpRequestDispatcher(urlField
  .getText(), "POST", this, encodedData.getBytes());
  dispatcher.start();
 }
 
 protected void makeMenu(Menu menu, int instance) {
  
  super.makeMenu(menu, instance);
  menu.add(new MenuItem("Post", 10, 10) {
   public void run() {
    postURL();
   }
  });
 }
 
 
 public void requestSucceeded(byte[] result, String contentType) {
  
  if (contentType.equals("image/png") || contentType.equals("image/jpeg") ||contentType.equals("image/gif")) {
     Bitmap bitmap = Bitmap.createBitmapFromBytes(result, 0, result.length, 1);
     synchronized (UiApplication.getEventLock()) {
      imageOutputField.setBitmap(bitmap);
     }
  }
  else if (contentType.startsWith("text/")) {
   String strResult = new String(result);
   synchronized (UiApplication.getEventLock()) {
    textOutputField.setText(strResult);
   }
  }
  else {
   synchronized (UiApplication.getEventLock()) {
    Dialog.alert("Unknown content type: " + contentType);
   }
  }
 }
 
 public void requestFailed(final String message) {
  UiApplication.getUiApplication().invokeLater(new Runnable() {
   public void run() {
    Dialog.alert("Request failed. Reason: " + message);
   }
  });
 }
}

 

PLEASE HELP ME TO FIX THE ERROR ...

 

 

Developer
Posts: 434
Registered: ‎07-23-2012
My Device: Blackberry 10
My Carrier: Orange

Re: http connection

[ Edited ]

Hi

i wll suggest you have a look on BB sample demo.

 

Please explore this site

https://github.com/blackberry/JDE-Samples

 

explore httpdemo sample example

Thanks
-------------------------------------------------------------------------------------
Press the Accept as solution Button when u got the Solution
Press Kudo to say thank to developer.
-------------------------------------------------------------------------------------.
Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: http connection

welcome to the support forums.

the code looks correct at a quick glance.
i would suggest that you check if you started the MDS simulator with your device simulator.

i would also suggest that you read a bit about the networking parameters, you are using ';deviceside=false' which means "use the BES connection" and requires the MDS simulator when used on the simulator.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Contributor
Posts: 8
Registered: ‎02-12-2013
My Device: all
My Carrier: ..

Re: http connection

i checked the option for the simulator in the "debug configurations "->simulator->launch MDS-CS with simulator
but also i keep have the same error
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: http connection

You have two choices now.  Either make sure the MDS starts correctly or change your connection to remove the requirement for MDS on the Simulator.  To do the later (which is what I do) change:

String url ="http://10.0.0.1;deviceside=false";

to

String url =http://10.0.0.1;deviceside=true;

 

Here is some more reading that I hope will help you to get a reliable connection:

 

http://supportforums.blackberry.com/t5/Java-Development/Networking-Transports-II/ta-p/446742

 

http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-make-an-HTTP-or-socket-con...

 

Having got this far, can I recommend that you review the ConnectionFactory API.  It seems to provide a good functional connection approach with fall back, for relatively few lines of code.