The codes below send a request to server and the server sends a response back. Then, the client formats the request and makes it a url that it loads on imageitem. This works fine using the emulator but as I deploy it on my mob, It is able to retrieve the string from my server but as it tries to load the image, it says NegativeArrayException.
Note:1. I have web already activated on my mob 2. When I use a static url, it works fine, that is instead of passing the formatted url, I pass a static url.
How do I get the image to load ?
public void run() {
try {
sendMessage("Query,map,$,start,211,Arsenal,!");
String unformattedurl = receiveMessage();
String url = "http://maps.google.com/maps/api/staticmap?center=100,20&zoom=14&size=500x400&format=JPEG&maptype=roadmap&sensor=false";
URL = getURL(unformattedurl);
stringItem.setText(URL);
Image image = loadImage(URL);
item.setImage(image);
} catch (Exception ex) {
stringItem.setText(stringItem.getText() + "Err2"+ex.getMessage());
}
}
private String getURL1(String message){
return "http://maps.google.com/maps/api/staticmap?center=Mauritius&zoom=10&size=200x200&maptype=roadmap&markers=color:Blue|label:U|22.0,22.0&markers=color:Green|label:B|21.0,21.0&sensor=false";
}
private String getURL(String message) throws Exception{
String[] messagearr=client.split(message,",");
String color,object;
String url="http://maps.google.com/maps/api/staticmap?center=Mauritius&zoom=10&size="+200+"x"+200+"&maptype=roadmap";
String lat,longitude;
if (messagearr[0].equals("query")){
if (messagearr[1].equals("Error")){
String error=messagearr[2];
}
else {
int startindex=5;
String distance,time;
distance=messagearr[2];
time=messagearr[3];
String name=messagearr[4];
String imageLabel="The bus is at :"+time+"min and "+distance +"km from user";
for (int i=startindex;i<messagearr.length;i+=2){
System.out.println("TEst8");
lat=messagearr[i];
longitude=messagearr[i+1];
if (i==startindex){
object="U";
color="Blue";
}
else{
object="B";
color="Green";
}
url=url+"&markers=color:"+color+"|label:"+object+"|"+lat+","+longitude;
item.setLabel(7+url);
}
url+="&sensor=false";
return url;
}
}
throw new Exception("Url is wrong");
}
private void sendMessage(String message) throws IOException{
DataOutputStream dos = connection.openDataOutputStream();
dos.writeUTF(message);
}
private String receiveMessage() throws IOException{
//stringItem.setText(stringItem.getText()+5);
DataInputStream dis = connection.openDataInputStream();
//stringItem.setText(stringItem.getText()+6);
return dis.readUTF();
}
public Image loadImage(String url) throws IOException {
HttpConnection hpc = null;
DataInputStream dis = null;
try {
hpc = (HttpConnection) Connector.open(url);
int length = (int) hpc.getLength();
System.out.println("Length is "+length);
byte[] data = new byte[length];
dis = new DataInputStream(hpc.openInputStream());
dis.readFully(data);
return Image.createImage(data, 0, data.length);
} finally {
if (hpc != null)
hpc.close();
if (dis != null)
dis.close();
}
}
}
This and my other question are the same except I have the problem defined in more detail. I also stored the URL in a stringItem and when I manually entered in into my broser I has the image loaded?
Aother thing when I tried hpc.getResponseMessage() I had an IOException thrown..