This can be done with a small one-liner addition
throw new IOException("Response:"+http.getResponseCode()+", "+http.getResponseMessage());
Here's the full code of the openUrl method
public String openUrl(String url) throws IOException, SecurityException{
byte[] data = null;
HttpConnection http = (HttpConnection)Connector.open(url, Connector.READ_WRITE, true);
//set custom headers
http.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
http.setRequestProperty("Accept_Language", "en-US");
//Read response
if(http.getResponseCode() == HttpConnection.HTTP_OK){
int len = (int)http.getLength();
is = http.openInputStream();
if(is == null){
throw new IOException("Cannot open HTTP InputStream, Aborting..");
}
//Else continue reading stream
response = "";
Reader in = new InputStreamReader(is);
StringBuffer temp = new StringBuffer(1024);
char[] buffer = new char[1024];
int read;
while((read = in.read(buffer, 0, buffer.length)) != -1){
temp.append(buffer, 0, read);
}
response = temp.toString();
}else{
System.out.println("Response:"+http.getResponseCode()+", "+http.getResponseMessage());
response = null;
throw new IOException("Response:"+http.getResponseCode()+", "+http.getResponseMessage());
}
if(http != null){
http.close();
}
if(is != null){
is.close();
}
return response;
}//--End of openUrl()
No comments:
Post a Comment