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
Highlighted
Contributor
Posts: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi

Getting 404 while sending file to URL

My app sends data to url address using post method in multiple steps (53 in total).. During this process I'm getting 404 error response from the server (randomly)... I'm using standard code nothing extraordinary, but for some reason it works until the point where it "decides to stop"...

try {
			addContentToPost("messageType", "FILE_PART");
			addContentToPost("toAddress", toAddress);//
			addContentToPost("fromAddress", fromAddress);//
			addContentToPost("fromName", fromName);//
			addContentToPost("MAC", MAC);//
			addContentToPost("fileName", filePartName);
			addContentToPost("fileSize", fileSize);
			addContentToPost("digidownVersion", appVersion);
			addContentToPost("filePartNumber", currentNumberOfFile);
			addContentToPost("fileNumberOfParts", partTotalNumber);
			addContentToPost("filePartSize", fileBytes.length);
			addContentToPost("Version", Version);
			addContentToPost("language", language);
			addContentToPost("phoneMAC", getPhoneMAC());
			addContentToPost("phoneName", System.getProperty("microedition.platform"));
			addContentToPost("session", MyActivity.getSession());
			
			
			
			addFileToPost(MAC + MyActivity.getSession() + filePartName);
			
		
			conn = (HttpConnection)Connector.open(MyActivity.getFinalUrl());
			
			setConnectionHeaders(conn);
			
			writeOutputStream(conn, os);
			
						
			listener.guiUpdateProgressBar(false,50,100);
			status = readInputStream(conn, is, 0);

		}			

			catch(Exception e1){
			
			debug("exception");
		}
	finally 
		
		{
				
				switch (status)
				{
				
				case STATUS_FAILED:
	debug("  FAILED TO send. Status - " + status);
					status = 0;
					break;

				case STATUS_OK:
					debug("File sent");
					status = 1;
					break;		
				}
				

 Is it blackberry bug or is there a way I can fix or avoid it..?

Thanks!

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting 404 while sending file to URL

There is nothing in the code that demonstrates where you are detecting the 404. 

 

A 404 is normally an HTTP error code that means page not found.  If you are always using the same URL and the error is not consistent, then I suspect this is going to be a touch one.

 

The only thing that immediately comes to mind is that you are swapping connection methods.  For example, if you were using WiFi inside your home, then the IP address 192.168.1.1 might be available to you.  But immediately you swap to wireless, you are connecting to a public gateway and 192.168.1.1 is not going to be available. 

 

I would make sure that you output the headers that are returned from all error status codes - the header will typically include the id of the Server that has returned the error code to you and that could help you figure out what this problem is. 

Contributor
Posts: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi

Re: Getting 404 while sending file to URL

[ Edited ]

Thanks Peter..

 

response 404 comes from

status = readInputStream(conn, is, 0);

...   line..

Can I open connection just once :

conn = (HttpConnection)Connector.open(MyActivity.getFinalUrl());

,use it until the whole file is sent and close it after the last part of the file is away ..?

Contributor
Posts: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi

Re: Getting 404 while sending file to URL

..I'm not sure if that will be available, cause I can see (using TCP dump) that during the process the app is using four connections simultaniously....

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting 404 while sending file to URL

"response 404 comes from
status = readInputStream(conn, is, 0);"

readInputStream is your method, it could just say "return 404;", and we wouldn't know.  I was just guessing the 404 was actually the http status, I don't know that from your code.  Unless you give us the code, or explain where that status actually comes from, we can't tell can we?

 

"Can I open connection just once :"

You can only use an http connection once. 

 

"I'm not sure if that will be available,"

What is 'that'?

 

" I can see (using TCP dump) that during the process the app is using four connections simultaniously"

TCP dump - is this something you are running on the Server?  Did you program the BB to use multiple Threads for multiple downloads. 

 

I think the place to start here is to track down exactly the circumstances in which you will see status 404 - I don't mean by testing, I mean by investigating your code and understanding when it will generate a 404. 

Contributor
Posts: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi

Re: Getting 404 while sending file to URL

[ Edited ]

My bad .. Sorry - too many questions and not enough info... Here is the function that reads inputstream after a part of message was sent :

byte readInputStream(HttpConnection conn, InputStream is, int expectedTensInt){

	try{
		try{
			try{
		Thread.sleep(1000);
			}catch(Exception r){ r.toString();}
		
		if (conn.getResponseCode();== HttpConnection.HTTP_OK) 
		{
			is = conn.openInputStream(); 
			
		//----------------------------- Reading response 

		do{
			len = is.read(data);
                        if(len > 0){
			debug("------");
			sb.append(new String(data, 0, len));
			debug(" " + sb.toString() + " ");
				}
		}while(len>0);
			
			sb.append("   "); 
			data=sb.toString().getBytes();
			debug("------");
			
		//--------------- End of reading response
			

	if (data.length <= 0){
		debug("Server failed to respond");
		return STATUS_FAILED;
	} else{

	if (serverResponse(expectedTensInt, 1, data, 0)){	
		debug("Server answered: SUCCESS");
		return STATUS_OK;

	} else if (serverResponse(expectedTensInt, 0, data, 0)){
		debug("Server answered: FAIL");
		return STATUS_RETRY;
					
	} else if (serverResponse(expectedTensInt, 3, data, 0)){
		debug("Server answered: 43");
		return STATUS_RETRY;
					
	} else {					
		debug("Server answered something not recognizable");
		return STATUS_RETRY;
	}
	}
        }
	else{

		do{
			len = is.read(data);
if(len > 0){
debug("------");
sb.append(new String(data, 0, len));
debug(" " + sb.toString() + " ");
	}while(len>0);
	sb.append("   "); 
	data=sb.toString().getBytes();
	debug("------");
	debug("Server's response was not HTTP_OK");
	
		}
		}
catch (NullPointerException e){
		debug("NULL Pointer exception: " + e.toString()+ " serverResponse: " + conn.getResponseCode() );
}
catch (Exception e){
	debug("HTTP exception: " + e.toString()+ " serverResponse: " + conn.getResponseCode());
	
}
	finally {
		closeInputStream(is);

	}
	
}

 ..it throws Null pointer exception and serverResponse as 404..

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting 404 while sending file to URL

[ Edited ]

When you say "serverResponse as 404", what you actually mean is conn.getResponseCode() == 404.  Correct?

 

For me, this code needs a bit of a work.

 

1) Forget the Thread sleep(), that is not helping anyone that I can see.

 

2) Replace your data reading code with code using IOUtilities.streamToBytes().  It is more efficient and gets the raw bytes, so doesn't suffer issues when the character encoding is not the standard Latin-1 that String defaults to. 

 

3) I can't understand your logic where you attempt a second read of your input stream if it is not what you expect.  You should read all if it at once. 

 

But none of this is actually going to help the 404.  We are back with the options I suggested in my initial post. 

Contributor
Posts: 47
Registered: ‎09-11-2013
My Device: different
My Carrier: wifi

Re: Getting 404 while sending file to URL

try {
	addContentToPost("messageType", "FILE_PART");
	addContentToPost("toAddress", toAddress);//
	addContentToPost("fromAddress", fromAddress);//
	addContentToPost("fromName", fromName);//
	addContentToPost("MAC", MAC);//
	addContentToPost("fileName", filePartName);
	addContentToPost("fileSize", fileSize);
	addContentToPost("appVersion", appVersion);
	addContentToPost("filePartNumber", currentNumberOfFile);
	addContentToPost("fileNumberOfParts", partTotalNumber);
	addContentToPost("filePartSize", fileBytes.length);
	addContentToPost("phoneVersion", phoneVersion);
	addContentToPost("language", language);
	addContentToPost("phoneMAC", getPhoneMAC());
	addContentToPost("phoneName", System.getProperty("microedition.platform"));
addContentToPost("session",MyActivity.getSession());
			
addFileToPost(MAC + MyActivity.getSession() + filePartName);
url = MyActivity.getFinalUrl();
conn = (HttpConnection)Connector.open(url);
setConnectionHeaders(conn);
		
writeOutputStream(conn, os);
listener.guiUpdateProgressBar(false,50,100);
		
status = readInputStream(conn, is, 0);

	}			
catch(Exception e1){} 
finally 
	{
		switch (status) {
				
	case STATUS_FAILED:
	case STATUS_RETRY:
		debug("***** FAILED TO SEND HTTP PACKET ");
		break;

	case STATUS_OK:
		debug("File PART sent");
		listener.httpCommUpdateBytes(fileBytes.length);
		listener.httpCommSucceeded();
		closeOutputStream(os);
		closeInputStream(is);
		closeConnection (conn);
		listener.EmailSent();
		running = false;
		break;		
	}
}

 This code sends parts of the file to the server... And line :

status = readInputStream(conn, is, 0);

 giving me this headache...

I've checked url before the code sends each part away - the url is ok.. Some times it fails to send, sometimes it send everything no probs.. The error comes up randomly. 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Getting 404 while sending file to URL

Isn't this just repeating what we already know?  What is new here?

 

As noted in my response to your previous post, from my perspective your options remain as they were initially, i.e.

 

"The only thing that immediately comes to mind is that you are swapping connection methods.  For example, if you were using WiFi inside your home, then the IP address 192.168.1.1 might be available to you.  But immediately you swap to wireless, you are connecting to a public gateway and 192.168.1.1 is not going to be available. 

 

I would make sure that you output the headers that are returned from all error status codes - the header will typically include the id of the Server that has returned the error code to you and that could help you figure out what this problem is. "

 

One other thing, are you aware that the suffix of your URL determines the connection method that the BlackBerry will try to use?  These are the options:

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

 

Are you deliberately choosing one of these suffixes?  Is it the same each time?