Hallo Zusammen,
ich beiße mir schon seit einiger Zeit die Zähne an ein Problem aus und hoffe, dass Ihr mir helfen könnt.
Hintergrund:
Ich möchte einen simplen Newsticker in Java basetln, der sich regelmäßig die aktuellen News aus der Seite
Google News holt.
Was ich bisher geschafft habe:
Ich konnte mir schon die Schlagzeilen, die Beitraglinks und die Bilderlinks durch folgenden Code aus der Seite
Google News holen:
Java Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
| package paketWebsiteReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
/**
* @author Administrator TODO Describe what the class is used for
*/
public class FilterStreamDemo
{
//***Felder / Attribute / Spalten definieren****************************************************************************************
//User-Agent definieren
//public final static String userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; drebaIE-DIT; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.1; MS-RTC LM 8)";
public final static String userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)";
//Alle Bilder einer html Seite auslesen.
public final static String REGEX_IMG = ".*<img.*src=\"(.*(jpg|jpeg|\")).*/>.*";
public final static Pattern PATTERN_IMG = Pattern.compile(REGEX_IMG);
//Nach allen Zeilen suchen die den Begriff "<span class="title text">" enthalten
public final static String REGEX_TEXT = ".*<span class=\"title.*text\">(.*?())</span></a>.*";
public final static Pattern PATTERN_TEXT = Pattern.compile(REGEX_TEXT);
//Links html Seite auslesen.
public final static String REGEX_LINK = ".*<a target=\".*href=\"(.*(/\"|html)).*>.*";
public final static Pattern PATTERN_LINK = Pattern.compile(REGEX_LINK);
public final static ArrayList<String> strArrTitle = new ArrayList<String>();
public final static ArrayList<String> strArrImage = new ArrayList<String>();
public final static ArrayList<String> strArrLink = new ArrayList<String>();
public static int indexTitle = 0;
public static int indexImage = 0;
public static int indexLink = 0;
public static int counterImage = 0;
public static int counterText = 0;
public static int counterLink = 0;
public static ImageIcon image = null;
//***Konstruktor-Methode definieren************************************************************************************************
FilterStreamDemo()
{
}
//*** Main-Methode definieren******************************************************************************************************
public static void main(String[] args)
{
try
{
URLConnection con = new URL("http://www.google.de/news").openConnection();
con.setRequestProperty("User-Agent",userAgent);
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = br.readLine()) != null)
{
// Remove special characters, such as ü
Pattern chue = Pattern.compile("ü");
Matcher msChue = chue.matcher(line);
while (msChue.find()) line = msChue.replaceAll("ü");
// Remove special characters, such as Ü
Pattern chueBig = Pattern.compile("Ü");
Matcher msChueBig= chueBig.matcher(line);
while (msChueBig.find()) line = msChueBig.replaceAll("Ü");
// Remove special characters, such as ö
Pattern choe = Pattern.compile("ö");
Matcher msChoe = choe.matcher(line);
while (msChoe.find()) line = msChoe.replaceAll("ö");
// Remove special characters, such as ä
Pattern chae = Pattern.compile("ä");
Matcher msChae = chae.matcher(line);
while (msChae.find()) line = msChae.replaceAll("ä");
// Remove special characters, such as ß
Pattern chsz = Pattern.compile("ß");
Matcher msChsz = chsz.matcher(line);
while (msChsz.find()) line = msChsz.replaceAll("ß");
// Remove special characters, such as <b>...</b>
Pattern chb = Pattern.compile(" <b>...</b>");
Matcher msChb = chb.matcher(line);
while (msChb.find()) line = msChb.replaceAll("");
// Remove special characters, such as „
Pattern chAnfuehrungszeichenUnten = Pattern.compile("„");
Matcher msAnfuehrungszeichenUnten = chAnfuehrungszeichenUnten.matcher(line);
while (msAnfuehrungszeichenUnten.find()) line = msAnfuehrungszeichenUnten.replaceAll("„");
// Remove special characters, such as “
Pattern chAnfuehrungszeichenOben = Pattern.compile("“");
Matcher msAnfuehrungszeichenOben = chAnfuehrungszeichenOben.matcher(line);
while (msAnfuehrungszeichenOben.find()) line = msAnfuehrungszeichenOben.replaceAll("“");
// Remove special characters, such as Ä
Pattern chaeBig = Pattern.compile("Ä");
Matcher msChaeBig = chaeBig.matcher(line);
while (msChaeBig.find()) line = msChaeBig.replaceAll("Ä");
//doFilterImages(line);
// doFilterText(line);
if(counterText!=counterImage || counterLink!=counterImage )
{
break;
}
doTitle(line);
doFilterImages(line);
doFilterLinks(line);
}
br.close();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
if (indexTitle >= strArrTitle.size() || indexImage >= strArrImage.size() || indexLink >= strArrLink.size())
{
indexTitle = 0;
System.out.println(strArrTitle.get(indexTitle));
indexImage = 0;
System.out.println(strArrImage.get(indexImage));
indexLink = 0;
//System.out.println(strArrLink.get(indexLink));
}
else
{
System.out.println(strArrTitle.get(indexTitle));
System.out.println(strArrImage.get(indexImage).substring(0,strArrImage.get(indexImage).indexOf("\"")));
System.out.println(strArrLink.get(indexLink).substring(0, strArrLink.get(indexLink).indexOf("\"")));
}
}
//*** Allgemeingültige-Methoden definieren*****************************************************************************************
/**
* @param line
* TODO Describe what the Method does
*/
private static void doFilterLinks(String line)
{
String mTextLink = null;
Matcher matcher = PATTERN_LINK.matcher(line);
if (matcher.matches())
mTextLink = matcher.group(1);
if (mTextLink !=null)
{
//System.out.println("Zeile-Link: " + counterLink + " " + mTextLink.substring(0, mTextLink.indexOf("\"")));
System.out.println("Zeile-Link: " + counterLink + " " + mTextLink);
counterLink++;
strArrLink.add(mTextLink);
}
}
/**
* @param line
* TODO Describe what the Method does
*/
private static void doFilterImages(String line)
{
String mTextImage = null;
Matcher matcher = PATTERN_IMG.matcher(line);
if (matcher.matches())
mTextImage = matcher.group(1);
if (mTextImage !=null)
{
System.out.println("Zeile-Image: " + counterImage + " " + mTextImage.substring(0, mTextImage.indexOf("\"")));
counterImage++;
strArrImage.add(mTextImage);
}
}
private static void doTitle(String line)
{
String mTextTitle = null;
Matcher matcher = PATTERN_TEXT.matcher(line);
if (matcher.matches())
mTextTitle = matcher.group(1).trim();
if (mTextTitle != null)
{
System.out.println("Zeile-Text: " + counterText + " " + mTextTitle);
counterText++;
strArrTitle.add(mTextTitle);
}
}
} |
So weit, so gut. Nun möchte ich mir mit Hilfe einer Visual Class in Eclipse eine entsprechende
GUI erstellen, die mir die jeweiligen Informationen darstellt. Die Bilder möchte ich folgendermaßen einem JLabel hinzufügen:
Java Code:
1
2
3
4
5
6
7
8
9
10
| try
{
image = new ImageIcon(new URL("http://nt0.ggpht.com/news/tbn/gHzWxuyQbirUgM/2.jpg"));
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
jLabelImage.setIcon(image); |
Das klappt auch soweit! So, und jetzt kommt´s! Die Seite
Google News liefert die Bilder nicht nur im Format: "
http://xy.jpg", sondern leider auch im Base64-Codierten Format, wie zum Beispiel:
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL..." (verkürzte Variante des Strings!).
Der Base64-Codierte String kann bei der Instanziierung der
URL-Klasse nicht verwendet werden. Also habe ich mich auf die Suche nach einem geeigneten Base64-Decoder gemacht. Den habe ich dann gefunden. Der Programmcode zum decodieren des Base64-Strings sieht folgendermaßen aus:
Java Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package paketWebsiteReader;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class MyBase64 {
public static void main(String[] args) throws Exception{
String googleString = "9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL..."; (verkürzte Variante des Strings!)
String entschluesselt = new String(new BASE64Decoder().decodeBuffer(googleString),"UTF-8");
System.out.println(entschluesselt); |
Voller Spannung habe ich dann auf das Ergebnis gewartet. Aber Pustekuchen! Die decodierte Version des Base64 Strings sieht wie folgt aus:
"'rI?=????Qy ?S?v?)#??Z??iã?c?\8?bke?Y;Q?3Rxt|??W??{?..." (verkürzte Variante des Ergebnisses!)
Lange Rede kurzer Sinn:
Was mache ich bei der Decodierung des Base64 Strings falsch? Oder gibt es eine andere Möglichkeit, um den String so zu decodieren, dass es bei der Instanziierung der
URL-Klasse verwendet werden kann?!
Hoffentlich könnt ihr mir helfen, da ich schon lange mit dem Problem zu kämpfen habe...!
Vielen Dank schon einmal im Voraus!!!