09-15-2012 03:59 AM
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.HEAD
HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_
+ ";boundary=" + BOUNDARY);
conn.setRequestProperty(HttpProtocolConstants.HEAD
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_"+getC
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
}
Solved! Go to Solution.
09-15-2012 06:24 AM
solved it..had some problem in my code..![]()