pouria_bayat
جمعه 05 فروردین 1390, 01:09 صبح
با سلام خدمت همه دوستان عزيز.
سوالي داشتم از اساتيد گرامي:
من با استفاده از كد زير تونستم سورس يك وبسايت رو در بيارم اما بعضي از نوشته هاي فارسي به صورت نامعلومي نوشته شدند ميخواستم بدنم چطور مي تونم اونهارو به UTF-8 در بيارم سورس كدي كه ازش استفاده كردم:
مثلا در هنگام نمايش سورس يك صفحه وب مطالبي كه فارسي هستش به صورت : ط¢ظ‡ظ† نمايش داده ميشه كه من ميخواهم به صورت درست نوشته بشه.
public String readResponse(HttpURLConnection urlConnection) throws IOException
{
BufferedReader bufferedReader = null;
try
{
// Prepare a reader to read the response from the URLConnection
// throws IOException
InputStream s = urlConnection.getInputStream();
byte buffer[] = new byte[BUFFER_SIZE];
int size = 0;
StringBuilder response = new StringBuilder();
do
{
size = s.read(buffer);
if (size != -1)
response.append(new String(buffer, 0, size));
} while (size != -1);
return response.toString();
}
catch(IOException ioException)
{
System.out.println("Problems while reading the response");
ioException.printStackTrace();
// throw the exception so that the caller is aware that
// there was some problems
throw ioException;
}
finally
{
// Good practice: clean up the connections and streams
// to free up any resources if possible
if (bufferedReader != null)
{
try
{
// throws IOException
bufferedReader.close();
}
catch(Throwable ignore)
{
// Cannot do much with exceptions doing clean up
// Ignoring all exceptions
}
}
}
}
ممنون ميشم اگر جواب بديد.لطفا دريغ نكنيد دوستان.
سوالي داشتم از اساتيد گرامي:
من با استفاده از كد زير تونستم سورس يك وبسايت رو در بيارم اما بعضي از نوشته هاي فارسي به صورت نامعلومي نوشته شدند ميخواستم بدنم چطور مي تونم اونهارو به UTF-8 در بيارم سورس كدي كه ازش استفاده كردم:
مثلا در هنگام نمايش سورس يك صفحه وب مطالبي كه فارسي هستش به صورت : ط¢ظ‡ظ† نمايش داده ميشه كه من ميخواهم به صورت درست نوشته بشه.
public String readResponse(HttpURLConnection urlConnection) throws IOException
{
BufferedReader bufferedReader = null;
try
{
// Prepare a reader to read the response from the URLConnection
// throws IOException
InputStream s = urlConnection.getInputStream();
byte buffer[] = new byte[BUFFER_SIZE];
int size = 0;
StringBuilder response = new StringBuilder();
do
{
size = s.read(buffer);
if (size != -1)
response.append(new String(buffer, 0, size));
} while (size != -1);
return response.toString();
}
catch(IOException ioException)
{
System.out.println("Problems while reading the response");
ioException.printStackTrace();
// throw the exception so that the caller is aware that
// there was some problems
throw ioException;
}
finally
{
// Good practice: clean up the connections and streams
// to free up any resources if possible
if (bufferedReader != null)
{
try
{
// throws IOException
bufferedReader.close();
}
catch(Throwable ignore)
{
// Cannot do much with exceptions doing clean up
// Ignoring all exceptions
}
}
}
}
ممنون ميشم اگر جواب بديد.لطفا دريغ نكنيد دوستان.