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: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone
Accepted Solution

Error: Return Code 3 while Uploading Images..

Hey All. I am recursively uploading some images. My code worked fine until i didnt implement synchronization in my code. Its now giving me Return Code: 3 , as the server response. Can anyone help me in this? Thanks.

 

public class FetchImg extends Thread {
private static int counter=0;
public void run(){
try{
//sending some image
sendImg(connectionStr.toString());
}
catch(Exception e){
System.out.println("====Exception : "+e.getMessage());
}
}

public static byte[] getBytesFromBitmap() {
//get bytes of next image
}

public synchronized void sendImg(String url){
final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy";
try{

byte[] imagedata=getBytesFromBitmap();

HttpConnection conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,
HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA
+ ";boundary=" + BOUNDARY);

conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
String.valueOf(imagedata.length));
conn.setRequestProperty("x-rim-transcode-content", "none");

ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream finalOut = conn.openOutputStream();

String newLine = "\r\n";
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(BOUNDARY.getBytes());
out.write(newLine.getBytes());
String contDisp = "Content-Disposition:form-data; name=\"uploadedfile\";filename=\"screenshot_"+getCounter()+".jpg\"";
String contEnc = "Content-Transfer-Encoding: binary";
String type = "Content-Type:image/jpeg";
out.write(contDisp.getBytes());
out.write(newLine.getBytes());
out.write(type.getBytes());
out.write(newLine.getBytes());
out.write(contEnc.getBytes());
out.write(newLine.getBytes());
out.write(newLine.getBytes());
out.write(imagedata);
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(BOUNDARY.getBytes());
out.write("--".getBytes());
out.write(newLine.getBytes());
finalOut.write(out.toByteArray());
out.flush();
out.close();
finalOut.flush();
finalOut.close();
InputStream instream=conn.openInputStream();
int ch=0;
StringBuffer buffesr=new StringBuffer();
while((ch=instream.read())!=-1)
{
buffesr.append((char)ch);
}
System.out.println("Server Response : "+new String(buffesr));
this.notifyAll();

} catch (Exception e) {
// TODO: handle exception
System.out.println("===Exception : "+e.getMessage());
}
}

 

 

 

public class ImageUploadApp extends UiApplication implements SystemListener{
public void powerUp() {
while(true){
FetchImg thrd=new FetchImg();
thrd.start();
synchronized(this){
try{
//Thread.sleep(4000);
this.wait();
}
catch(InterruptedException e){
System.out.println("====InterruptedException : "+e.getMessage());
}
}
}
}
//other methods
}

Developer
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Re: Error: Return Code 3 while Uploading Images..

solved it..had some problem in my code..Smiley Happy