HuyPV
Friday, December 31, 2010
public String getSourceFromUrl() throws IOException {
//if (true) return "I love google";
String strRet = "";
HttpConnection httpConn = null;
String url = "http://huypv.net/t/xs.php";
InputStream is = null;
OutputStream os = null;
try {
// Open an HTTP Connection object
httpConn = (HttpConnection) Connector.open(url);
// Setup HTTP Request
httpConn.setRequestMethod(HttpConnection.GET);
httpConn.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Confirguration/CLDC-2.0");
/** Initiate connection and check for the response code. If the
response code is HTTP_OK then get the content from the target
**/
int respCode = httpConn.getResponseCode();
if (respCode == httpConn.HTTP_OK) {
StringBuffer sb = new StringBuffer();
os = httpConn.openOutputStream();
is = httpConn.openDataInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
int chr;
while ((chr = isr.read()) != -1) {
sb.append((char) chr);
}
strRet = sb.toString();
System.out.println("Response " + strRet);
} else {
System.out.println("Error in opening HTTP Connection. Error#" + respCode);
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (httpConn != null) {
httpConn.close();
}
}
return strRet;
}
Title:
J2ME Get Unicode HTML source
Description:
public String getSourceFromUrl() throws IOException { //if (true) return "I love google"; String strRet = ""; ...
...
Rating:
4