I'm getting a MalformedURLException some code in my Android Studio project. My aim is to get and display an image from a web page, and the URL seems to be fine, but it's giving me this error.
I have already put the code in a try,catch, but that is still giving me the error.
Here is the code to grab the image and display it:
try
{
url = new URL(items.get(position).getLink());
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
}
catch (IOException e)
{
throw new RuntimeException("Url Exception" + e);
}
holder.itemTitle.setText(items.get(position).getTitle());;
holder.itemHolder.setBackgroundDrawable(new BitmapDrawable(bmp));
items.get(position).getLink() is meant to get the link that is being displayed in a ListView, but even something like URL url = new URL("https://www.google.com") doesn't work.
Thanks.
your url is formatted without the protocol at the beginning try
url = new URL("http://"+items.get(position).getLink());
sometimes the url string may have special characters, in which case you need to encode it properly please see this.
And also, the url you posted in the comment is not an image.
it is exception beacuse of url class
add antoher catch like
catch (MalformedURLException e) {
e.printStackTrace();
}
Just click alt+enter and then import try catch section... this helped me...
Related
have a problem here with Java. I'm a beginner and I don't know why it's not working.
What I want:
I have an homepage, I want to change some properties of a module. Therefore I have an .php-file. Going to the URL (looks like http://subdomain.domain.de/path_to_module/file.php?property=XY&api=Z) is all I have to do. To make this faster and easier especially also for friends, I want to code a little app.
I looked up google and get so far:
public void onClick(View v) {
try {
URL url = new URL("http://subdomain.domain.de/path_to_module/file.php?
property=XY&api=Z");
HttpURLConnection connection = null ;
try {
connection = (HttpURLConnection) url.openConnection();
}
finally
{
if( connection != null )
connection.disconnect() ;
}
} catch (MalformedURLException e){
} catch (IOException e){}
}
But that isn't working. The App don't crash or anything. It looks like it works, but when you look on the website it isn't changing anything.
Go to the URL by hand is working, so this can't be the problem...
Do you have any ideas, how to solve the problem?
Please excuse bad spelling or something like that, I'm not a native speaker.
Hey #David Conrad thanks for your help.
If I get you right, the new Code should look like this:
public void onClick(View v) {
try {
URL url = new URL("http://subdomain.domain.de/path_to_module/file.php?
property=XY&api=Z");
HttpURLConnection connection = null ;
try {
connection = (HttpURLConnection) url.openConnection();
connection.connect();
}
finally
{
if( connection != null )
connection.disconnect() ;
}
} catch (MalformedURLException e){
} catch (IOException e){}
}
I tried it, but it didn't work. Did I missunderstand what you are talking about?
The URL openConnection() method doesn't make the connection:
It should be noted that a URLConnection instance does not establish the actual network connection on creation. This will happen only when calling URLConnection.connect().
You have to call URLConnection.connect() on the HttpURLConnection object it returns.
Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.
The HttpURLConnection doesn't begin transferring data right away because you may want to set HTTP headers, or you may want to do a POST or a PUT instead of a GET. You may have to get the input stream, and possibly even read from it, to cause it to make a GET request from the server (if that is what you are trying to do).
InputStream is = connection.getInputStream();
is.transferTo(new ByteArrayOutputStream());
This will read all the data from the connection and write it to a buffer, which you could discard if you don't care about its contents.
I try to create a PDF with iText within my JSP-Application and want to insert an image.
The file is located in my webapp-directory under:
http://localhost:8087/Buran/Symbols/Logos/dobi1.jpg
It's no problem to use it in html or jsp-files but within the bean it just does not work. To get the correct path by trial and error I created a test-file to see from which directory I had to start. Unfortunately that's not really what I expected it to be:
./apache-tomcat-7.0.41/bin/testfile.test
I got a recommendation to use:
private final String dobiurl = "/Buran/Symbols/Logos/dobi1.jpg";
URLConnection connection;
/**/
try {
connection = new URL(dobiurl).openConnection();
} catch (IOException ex) {
Logger.getLogger(Etikette.class.getName()).log(Level.SEVERE, null, ex);
}
But just cause errors...
I have a button to open a URL in browser:
URI uri = new URI("http://google.com/");
Desktop dt = Desktop.getDesktop();
dt.browse(uri.toURL()); // has error
but i get following error in last statement:
The method browse(URI) in the type Desktop is not applicable for the arguments (URL)
thanks for any suggestion.
Found solution:
1. Remove .toURL()
2. use try catch block
try
{
URI uri = new URI("http://google.com/");
Desktop dt = Desktop.getDesktop();
dt.browse(uri);
}
catch(Exception ex){}
What it tells you is that you are sending an URL object while it expects an URI.
just change
dt.browse(uri.toURL()); // has error
to
dt.browse(uri); // has error
before being able to use Desktop, you have to consider if it is supported
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
// now enable buttons for actions that are supported.
enableSupportedActions();
}
and the enableSupportedActions
private void enableSupportedActions() {
if (desktop.isSupported(Desktop.Action.BROWSE)) {
txtBrowserURI.setEnabled(true);
btnLaunchBrowser.setEnabled(true);
}
}
that shows that you have to check also, if BROWSE action is also supported.
use some thing like this
try {Desktop.getDesktop().browse(new URI("http://www.google.com"));
} catch (Exception e)
{JOptionPane.showMessageDialog(null,e);}
}
This shouldn't be that hard, but I cannot figure this out. I need to save an image, on my end only, and build it dynamically so all my users will view these images. The tutorials on Parse.com are very helpful, but not in the case of images. I need detailed explanations or helpful links. Thanks for looking.
This is all I have so far as far as saving an image. I am properly getting the file in my Data Browser, but if I try to view it, it only shows my string "beatdown.jpg" not the actual jpg.
....
private void saveImage() {
// TODO Auto-generated method stub
InputStream header = new FileInputStream("beatdown.jpg");
byte[] head = IOUtils.toByteArray(header);
ParseFile file = new ParseFile(head);
try{
file.save();
} catch (ParseException e) {
e.printStackTrace();
}
ParseObject displayImage = new ParseObject("displayImage");
displayImage.put("header", file);
try{
displayImage.save();
} catch (ParseException e1){
e1.printStackTrace();
}
}
I understand I am trying to get the string of "beatdown.jpg" to bytes in the code above, and it is not handling it as a .jpg. But I don't know how to make it a .jpg.
EDIT: I added commons-io. But when I run the code (see the above updated code), it won't register on anything on parse.com. I am getting this in my logcat;
Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection#40cf2498 that was originally bound here
The key elements are:
File f = new File("pathToFile");
FileInputStream fis = new FileInputStream(f);
byte[] bytes = new byte[f.length()];
fis.read(bytes);
Of course there's exception handling and the like to do, but this should be enough to give you the general idea.
I am having a problem writing to a .xml file inside of my jar. When I use the following code inside of my Netbeans IDE, no error occurs and it writes to the file just fine.
public void saveSettings(){
Properties prop = new Properties();
FileOutputStream out;
try {
File file = new File(Duct.class.getResource("/Settings.xml").toURI());
out = new FileOutputStream(file);
prop.setProperty("LAST_FILE", getLastFile());
try {
prop.storeToXML(out,null);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.toString());
}
try {
out.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.toString());
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.toString());
}
}
However, when I execute the jar I get an error saying:
IllegalArguementException: uri is not hierachal
Does anyone have an idea of why it's working when i run it in Netbeans, but not working when i execute the jar. Also does anyone have a solution to the problem?
The default class loader expects the classpath to be static (so it can cache heavily), so this approach will not work.
You can put Settings.xml in the file system if you can get a suitable location to put it. This is most likely vendor and platform specific, but can be done.
Add the location of the Settings.xml to the classpath.
I was also struggling with this exception. But finally found out the solution.
When you use .toURI() it returns some thing like
D:/folderName/folderName/Settings.xml
and hence you get the exception "URI is not hierarchical"
To avoid this call the method getPath() on the URI returned, which returns something like
/D:/folderName/folderName/Settings.xml
which is now hierarchical.
In your case, the 5th line in your code should be
File file = new File(Duct.class.getResource("/Settings.xml").toURI().getPath());