Quote:
Originally Posted by amitinc
I hope the thread doesn't die without any reply............ Has nobody got some answer to such questions in past ? ....If yes plz help me out...
|
well... the device SHOULD give you back a response if data has been received successfully... check out this sample code.... around the if (content
HOWEVER... i have not yet seen a contnent length being anything but 0. I assume its due to me running on simulator. Can someone confirm that??
SERVER CODE
import java.io.*;
import java.net.*;
import java.util.*;
class HTTPPushSDemo {
//constants
private static final String HANDHELD_EMAIL = "2100000A";
private static final String HANDHELD_PORT = "100";
private static final String BES_HOST = "localhost";
private static final int BES_PORT = 8080;
private static final String CONTENT = "c:/landskronagade.png";
//constructor
public HTTPPushSDemo() {
}
private static URL getPushURL(String HandheldEmail) {
URL _pushURL = null;
try {
if ((HandheldEmail == null) || (HandheldEmail.length() == 0)) {
HandheldEmail = HANDHELD_EMAIL;
}
_pushURL = new URL("http", BES_HOST, BES_PORT,
"/push?DESTINATION="+ HandheldEmail
+"&PORT="+HANDHELD_PORT+"&REQUESTURI=/");
} catch (MalformedURLException e) {
System.err.println(e.toString());
}
return _pushURL;
}
public static void postData(byte[] data) {
try {
URL url = getPushURL(HANDHELD_EMAIL);
System.out.println("Sending to" + url.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true); //for receiving the confirmation
conn.setDoOutput(true); //for sending the data
conn.setRequestMethod("POST"); //post the data to the BES
OutputStream out = conn.getOutputStream();
out.write(data); //write the data
out.close();
InputStream ins = conn.getInputStream();
int contentLength = conn.getContentLength();
System.out.println("Content length: "+ contentLength);
if (contentLength > 0) {
byte[] someArray = new byte [contentLength];
DataInputStream dins = new DataInputStream(ins);
dins.readFully(someArray);
System.out.println(new String(someArray));
} ins.close();
conn.disconnect();
} catch (IOException e) {
System.err.println(e);
}
}
public static void main (String args[]) {
try {
File f = new File(CONTENT);
if ( f == null ) {
throw new RuntimeException("Unable to Open File");
}
FileInputStream fi = new FileInputStream(f);
if ( null == fi ) {
throw new RuntimeException("Unable to open file");
}
int size = fi.available();
byte[] imageData = new byte[size];
int bytesRead = fi.read(imageData);
fi.close();
postData(imageData);
} catch (IOException e) {
System.err.println(e.toString());
}
}
}
CLIENT CODE
/**
* HTTPPushDemo.java
* Copyright (C) 2001-2004 Research In Motion Limited. All rights reserved.
*/
package com.rim.samples.docs.httppush;
import java.io.*;
import javax.microedition.io.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.i18n.*;
import net.rim.device.api.system.*;
import net.rim.device.api.util.*;
import com.rim.samples.docs.baseapp.*;
public class HTTPPushDemo extends BaseApp {
// Constants.
private static final String URL = "http://:100";
private static final int CHUNK_SIZE = 256;
// Fields.
private ListeningThread _listeningThread;
private MainScreen _mainScreen;
private RichTextField _infoField;
private BitmapField _imageField;
public static void main(String[] args) {
HTTPPushDemo theApp = new HTTPPushDemo();
theApp.enterEventDispatcher();
}
/**
* Create a separate listening thread so that you do not
* block the application's main event thread.
*/
private class ListeningThread extends Thread {
private boolean _stop = false;
private StreamConnectionNotifier _notify;
public synchronized void stop() {
_stop = true;
try {
_notify.close(); // Close the connection so thread returns.
} catch (IOException e) {
System.err.println(e.toString());
} catch (NullPointerException e) {
// The notify object likely failed to open, due to an IOException.
}
}
public void run() {
StreamConnection stream = null;
InputStream input = null;
try {
synchronized(this) {
// Open the connection once or re-open after an IOException.
_notify = (StreamConnectionNotifier)Connector.open(URL);
}
while (!_stop) {
// NOTE: This method blocks until data is received.
stream = _notify.acceptAndOpen();
input = stream.openInputStream();
// Extract the data from the input stream.
DataBuffer db = new DataBuffer();
byte[] data = new byte[CHUNK_SIZE];
int chunk = 0;
while ( -1 != (chunk = input.read(data)) ) {
db.write(data, 0, chunk);
}
input.close();
data = db.getArray();
updateBitmap(data);
}
} catch (IOException e) {
System.err.println(e.toString()); // It is likely the stream was closed.
}
}
}
// Constructor.
public HTTPPushDemo() {
_mainScreen = new MainScreen();
_mainScreen.setTitle(new LabelField("Latest Logos", LabelField.USE_ALL_WIDTH));
_infoField = new RichTextField();
_mainScreen.add(_infoField);
_mainScreen.add(new SeparatorField());
_imageField = new BitmapField(null, BitmapField.HCENTER|BitmapField.TOP);
_mainScreen.add(_imageField);
_mainScreen.addKeyListener(this);
_mainScreen.addTrackwheelListener(this);
_listeningThread = new ListeningThread();
_listeningThread.start();
_infoField.setText("Application is listening...");
pushScreen(_mainScreen);
}
private void updateBitmap(final byte[] data) {
Application.getApplication().invokeLater(new Runnable() {
public void run() {
// Query the user to load the received image.
String[] choices = {"OK", "CANCEL"};
if ( 0 != Dialog.ask("Do you want to display latest logo?",
choices, 0) ) {
return;
}
_infoField.setText("Image received. Size: "+ data.length);
_imageField.setBitmap(Bitmap.createBitmapFromPNG(d ata, 0,data.length));
}
});
}
protected void onExit() {
// Stop the listening thread.
_listeningThread.stop();
try {
_listeningThread.join();
} catch (InterruptedException e) {
System.err.println(e.toString());
}
}
}