1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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
| import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
public class TestEncodage {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//URL à parser
String lien="http://www.google.com/ig/api?weather="
+URLEncoder.encode("paris", "UTF-8")
+",france&hl=fr";
//Construction du StringBuilder
URL url = new URL (lien);
BufferedReader br = null;
StringBuffer stringBuilder = null;
try {
InputStreamReader reader = new InputStreamReader (url.openConnection ().getInputStream (), "UTF-8");
br = new BufferedReader (reader);
String x = reader.getEncoding();
System.out.println("\nencoding is "+x);
String line = null;
stringBuilder = new StringBuffer();
while((line=br.readLine())!=null){
byte[] test = line.getBytes("UTF-8");
stringBuilder.append(new String(test));
}
}
finally{
if (br != null)
br.close();
}
String resultat=stringBuilder.toString();
System.out.println(resultat);
}
} |