2016-03-24 7 views
0

Ich versuche, ein Bild im Hinblick auf eine Bitmap aus einer URL genommen zu setzen, aber die folgende Stack-Trace angezeigt, nachdem ich den folgenden Code ausführen:MalfomedURLException Protokoll nicht gefunden

public Bitmap getAlbumCover(Context context, String song, String artist) { 
    this.context = context; 

    song = song.replace(" ", "%20"); 
    artist = artist.replace(" ", "%20"); 

    final String finalSong = song; 
    final String finalArtist = artist; 

    Thread thread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       ObjectMapper mapper = new ObjectMapper(); 
       Document doc = Jsoup.connect("http://api.spotify.com/v1/search?q=track:" + finalSong + "%20artist:" + finalArtist+"%20" + "&type=track").ignoreContentType(true).get(); 
       String body = String.valueOf(doc.body().text()); 
       JsonNode readTree = mapper.readTree(body); 
       albumArt = readTree.findPath("url"); 

       try { 
        URL url = new URL(albumArt.toString()); 
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
        connection.setDoInput(true); 
        connection.connect(); 
        InputStream input = connection.getInputStream(); 
        albumImage = BitmapFactory.decodeStream(input); 
       } catch (Exception e) { 
        Log.e("Error", e.getMessage()); 
        e.printStackTrace(); 
       } 

      } catch (JsonGenerationException e) { 
       e.printStackTrace(); 
      } catch (JsonMappingException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    thread.start(); 

    Log.i("icon", "i"+ albumImage); 
    return albumImage; 
} 

Hier ist der Stack-Trace:

03-23 20:18:43.242 6917-6991/.xyz.php_request W/nAnnotationIntrospector: Unable to load JDK7 annotation types; will have to skip 
03-23 20:18:43.943 6917-6991/.xyz.php_request E/Error: Protocol not found: "https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab" 
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err: java.net.MalformedURLException: Protocol not found: "https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab" 
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err:  at java.net.URL.<init>(URL.java:176) 
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err:  at java.net.URL.<init>(URL.java:125) 
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err:  at pillo.xyz.php_request.songlistadapter$1.run(songlistadapter.java:124) 
03-23 20:18:43.943 6917-6991/.xyz.php_request W/System.err:  at java.lang.Thread.run(Thread.java:818) 

Der Link lautet hier: https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab Wenn Sie darauf klicken, es ein Bild ist nach oben ziehen. Was mache ich falsch? Ich habe bereits versucht, die URL in UTF-8 zu kodieren.

EDIT:

Logging Albumcover kehrt: I/Albumcover: "https://i.scdn.co/image/bf44daf8c3f35de83e5875bc49791c1347ef36f0"

+0

Ich kann Ihnen sagen, das Bild kann gelesen werden - das Problem ist wahrscheinlich mit dieser Zeile albumImage = BitmapFactory.decodeStream (Eingabe); – gpasch

+0

ok, also was denkst du ist falsch damit? –

+0

es zeigt es so in vielen totorials –

Antwort

1

Was mache ich falsch?

Sie haben in dieser Zeile die falsche Methode albumArt aufgerufen: URL url = new URL(albumArt.toString());. Sie sollten albumArt.getTextValue() statt albumArt.toString() anrufen.

Warum:
toString() die Zeichenfolge Format des JsonNode gibt, die in Ihrem Fall Zeichenfolge " \" https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab \ "".

Stattdessen gibt getTextValue() die Zeichenfolge "https://i.scdn.co/image/9c2c4a9ac9726bfd996ff96383178bbb5efc59ab" das ist, was URL will.

Den Unterschied sehen?

Verwandte Themen