الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

[تنبيه - عنوان غير مناسب : ]لو سمحتم مساعدة عاجلة

بدأه fall في 4 يوليو 2009 · 8 رد · 929 مشاهدة · في JavaME
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه ،،

أنا مشروعي هو بناء متصفح إنترنت للأجهزة المحمولة و هوي على قد إمكانياتي بس المشكلة أنو عم يجيب الصفحات العربية مشفرة مع أني استخدمت مايلي

hc.setRequestProperty("Accept-charset", "Windows-1256, Win. x-cp1256, cp1256, cp-1256");

و

hc.setRequestProperty("Accept-charset", "utf-8");

حيث hc هو الاتصال

بس ما مشي الحال بالإضافة إلى أني جربت كل شي بيتعلق بالعربي متل

MacArabic

و غيرها

رجاءً ساعدوني و مشكورين جزيلاً

أختكم fall

#2

الأخ الكريم/الأخت الكريمة

السلام عليكم ورحمة الله وبركاته

مرحباً بكم في منتدى الفريق العربي للبرمجة

نود تنبيهك أن العنوان غير مناسب.

الرجاء اختيار عنوان مناسب أكثر في مواضيعك القادمة، وهذا بمثابة إشعار للأهمية.

وفي حال التكرار سيتعرض موضوعك للإغلاق والحذف!!!

قواعد المشاركة

/index.php?showtopic=29343

شاكرين لكم حُسن تعاونكم

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3

الخطأ ليس في ال HttpConnection

الخطأ في قرأة ال inputStream

جرب التالي:

byte[] b=new byte[1];
while(in.read(b)!=-1){
String str=new String(b,"utf-8");
strBuffer+=str;
}

و بالتوفيق

#4

شكراً لك اخي الكريم

بس عم يعطي

NullPointerException

يعني StringBuffer ما عم تتعبّا

#6

رح حطلك الكود أخي الكريم و لو لأنو طويل كتيييير

أول شي عندي الصف ConnectFormالمسؤول عن تأسيس الاتصال و إحضار البيانات من صفحة الويب على شكل مصفوفة بايتاتو هو بالشكل التالي

class InternetConnection extends Thread

{

public void run()

{

Connection c = null;

InputStream is = null;

try {

try {

int len = -1;

if (url.startsWith("jar://"))

{

is = this.getClass().getResourceAsStream(url.substring(6));

}

else if (url.startsWith("file://") && !hasFileConnection)

{

is = this.getClass().getResourceAsStream(url);

}

else

{

c = Connector.open(url);

connection = c;

if (c instanceof HttpConnection)

{

HttpConnection hc = (HttpConnection)c;

hc.setRequestMethod(HttpConnection.GET);

hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");

hc.setRequestProperty("Accept-charset", "utf-8");

hc.setRequestProperty("Content-Language", ConnectForm.LANGUAGE_CODE);

is = hc.openInputStream();

if (stopped)

{

return;

}

String type = hc.getType();

len = (int)hc.getLength();

}

else

{

is = ((InputConnection)c).openInputStream();

}

}

byte[] body;

if (c != null && "com.mot.cldc.io.j2me.http.Protocol".equals(c.getClass().getName()))

{

if (len >= 0)

{

gauge.setMaxValue(len);

body = new byte[len+4];

int offs = 0;

while (offs < len)

{

int rc = is.read(body, offs, len - offs);

if (stopped)

{

return;

}

if (rc >= 0)

{

offs += rc;

gauge.setValue(offs);

}

else

{

browser.error(ConnectForm.CONNECTION_FAILURE);

return;

}

}

}

else

{

int rc;

body = new byte[iNIT_BUF_SIZE];

for (len = 0; !stopped && (rc = is.read(body, len, body.length-len-4)) >= 0;)

{

if (rc == 1)

{

if (body[len] == -1)

{

break;

}

}

len += rc;

if (len+4 == body.length)

{

int newLen = (len+4)*2 < BUF_SIZE_EXTENT ? (len+4)*2 : len + 4 + BUF_SIZE_EXTENT;

byte[] newBody = new byte[newLen];

System.arraycopy(body, 0, newBody, 0, len);

body = newBody;

}

}

}

}

else

{

// normal implementation

if (len >= 0)

{

gauge.setMaxValue(len);

body = new byte[len];

int offs = 0;

while (offs < len)

{

int rc = is.read(body, offs,len - offs);

if (stopped)

{

return;

}

if (rc >= 0)

{

offs += rc;

gauge.setValue(offs);

}

else

{

browser.error(ConnectForm.CONNECTION_FAILURE);

return;

}

}

}

else

{

int rc;

body = new byte[iNIT_BUF_SIZE];

for (len = 0; !stopped && (rc = is.read(body, len, body.length-len)) >= 0;)

{

len += rc;

if (len == body.length)

{

int newLen = len*2 < BUF_SIZE_EXTENT ? len*2 : len + BUF_SIZE_EXTENT;

byte[] newBody = new byte[newLen];

System.arraycopy(body, 0, newBody, 0, len);

body = newBody;

}

}

}

}

if (!stopped)

{

new ReaderCanvas(url, pos, label, body, len, browser);

}

}

finally

{

if (is != null)

{

is.close();

}

if (c != null)

{

c.close();

}

}

}

catch (Exception x)

{

x.printStackTrace();

System.out.println("Exception " + x);

browser.error(ConnectForm.CONNECTION_FAILURE);

}

}

}

حطيتلك بس الصف الداخلي المسؤول عن الاتصال

بعدين مصفوفة البايتات بتنبعت للصف ReaderCanvas اللي هو بمثابة بارزر لأوسمة صفحة الويب بالشكل التالي

static final String[] breakTags =

{

"li", "br", "tr", "p", "h1", "h2", "h3", "hr", "/p", "/h1", "/h2", "/h3" };

static final String[] skipTags =

{"form", "script", "style" };

void convertText()

{

byte[] t = text;

int length = textLength;

int src, dst;

int skip = 0;

int preserve = 0;

for (src = 0, dst = 0; src < length; src++)

{

byte b = t[src];

if (b == '<') // start of a tag b=t[0]

{

if (src+3 < length && t[src+1] == '!' && t[src+2] == '-' && t[src+3] == '-') // comment tag

{

src += 3;

do

{

src += 1;

}

while (src+3 < length && !(t[src] == '-' && t[src+1] == '-' && t[src+2] == '>'));

src += 2;

continue;

}

int start = src+1;

while (++src < length && t[src] != '>') // reading the tag name

{

if (t[src] == '<')

{

start = src+1;

}

}

String element = new String(t, start, src-start); // the tag name is ready

String tagName = element.toLowerCase(); // the tag name is tagName

for (int j = 0; j < skipTags.length; j++) // comparing with skipTags

{

String st = skipTags[j];

if (tagName.equals(st)

|| (tagName.startsWith(st) && tagName.charAt(st.length()) == ' '))

{

skip += 1;

break;

}

}

if (skip > 0 && tagName.startsWith("/"))

{

String suf = tagName.substring(1);

for (int j = 0; j < skipTags.length; j++)

{

if (suf.equals(skipTags[j]))

{

skip -= 1;

break;

}

}

}

if (skip > 0)

{

continue;

}

if (tagName.startsWith("pre"))

{

preserve += 1;

}

else if (tagName.startsWith("/pre"))

{

preserve -= 1;

}

else if (tagName.startsWith("a href"))

{

String url = getUrl(element);

if (url.length() > 0 && !url.equals("#"))

{

tags.link(new Tag(url, dst));

}

}

else if (tagName.startsWith("a name"))

{

int qs = tagName.indexOf('"');

int qe = tagName.lastIndexOf('"');

if (tagName.substring(qs+1, qe).equals(label))

{

currPagePos = dst;

}

index.put(getUrl(element), new Integer(dst));

}

else if (tagName.equals("/a"))

{

if (tags.prev.end < 0)

{

if (tags.prev.beg == dst)

{

tags.prev.unlink();

}

else

{

tags.prev.end = dst;

}

}

}

else

{

for (int j = 0; j < breakTags.length; j++)

{

String bt = breakTags[j];

if (tagName.equals(bt)

|| (tagName.startsWith(bt) && tagName.charAt(bt.length()) == ' '))

{

if (bt.equals("p"))

{

for (int i = 0; i < INDENTATION; i++)

{

t[dst++] = ' ';

}

}

else if (dst == 0 || t[dst-1] == '\n')

{

continue;

}

t[dst++] = '\n';

break;

}

}

}

}

else if (skip == 0)

{

if (b == '&')

{

int j, n = src + 8 < length ? src + 8 : length;

for (j = src+1; j < n && t[j] != ';'; j++);

if (j < n && t[j] == ';')

{

String symStr = new String(t, src+1, j-src-1).toLowerCase();

src = j;

char ch;

if (symStr.equals("amp"))

{

ch = '&';

}

else if (symStr.equals("quot"))

{

ch = '"';

}

else if (symStr.equals("lt"))

{

ch = '<';

}

else if (symStr.equals("gt"))

{

ch = '>';

}

else if (symStr.equals("nbsp"))

{

ch = ' ';

}

else if (symStr.startsWith("#"))

{

ch = (char)Integer.parseInt(symStr.substring(1), 16);

}

else

{

continue;

}

t[dst++] = (byte)ch;

}

else

{

t[dst++] = '&';

}

}

else if (b == '\n' || b == ' ' || b == '\t')

{

if (preserve != 0)

{

t[dst++] = (byte)((b == '\n') ? '\r' : ' ');

}

else

{

if (dst != 0 && t[dst-1] != ' ' && t[dst-1] != '\n')

{

t[dst++] = ' ';

}

}

}

else if (b != '\r')

{

t[dst++] = b;

}

}

}

textLength = dst;

}

static void insertSpaces(StringBuffer buf, int nSpaces)

{

if (nSpaces > 0)

{

int i = 0;

int n = buf.length();

while (buf.charAt(i) == ' ') {

i += 1;

}

int nSeparators = 0;

while (++i < n)

{

if (buf.charAt(i) == ' ')

{

nSeparators += 1;

}

}

if (nSeparators != 0)

{

int ns1 = nSpaces / nSeparators;

int ns2 = nSpaces % nSeparators;

do {

if (buf.charAt(--i) == ' ')

{

for (int j = 0; j < ns1; j++)

{

buf.insert(i, ' ');

}

if (ns2 != 0)

{

buf.insert(i, ' ');

ns2 -= 1;

}

nSeparators -= 1;

}

}

while (nSeparators != 0);

}

}

}

int splitWord(StringBuffer buf, int n, int width, boolean align)

{

char c1 = buf.charAt(n-1);

char c2 = buf.charAt(n-2);

if (isSeparator(c2))

{

if (c2 == ' ')

{

buf.delete(n-2, buf.length());

width -= spaceWidth + font.charWidth(c1);

}

else

{

buf.delete(n-1, buf.length());

width -= font.charWidth(c1);

}

if (align)

{

insertSpaces(buf, (screenWidth - width)/spaceWidth);

}

return 1;

}

int i = ConnectForm.split(buf, n);

if (i <= 0)

{

i = n-2;

}

int rest = n-i;

boolean insertHyphen = true;

if (i >= 1 && isSeparator(buf.charAt(i-1)))

{

if (buf.charAt(i-1) == ' ')

{

i -= 1;

}

insertHyphen = false;

}

else if (i >= 2 && isSeparator(buf.charAt(i-2)))

{

i -= buf.charAt(i-2) == ' ' ? 2 : 1;

rest += 1;

insertHyphen = false;

}

for (int j = i; j < n; j++)

{

width -= font.charWidth(buf.charAt(j));

}

buf.delete(i, buf.length());

if (insertHyphen)

{

buf.insert(i, ConnectForm.HYPHEN);

width += hyphenWidth;

}

if (align)

{

insertSpaces(buf, (screenWidth - width)/spaceWidth);

}

return rest;

}

protected void paint(Graphics g)

{

g.setColor(BACKGROUND_COLOR);

g.fillRect(g.getClipX(), g.getClipY(), g.getClipWidth(), g.getClipHeight());

boolean containsCurrTag = false;

while (true)

{

int pos = currPagePos;

boolean eof = false;

Tag tag = tags.next;

Tag firstPageTag = null;

nLines = 0;

for (int i = 0; !eof && i < screenLines; i++)

{

linePos = pos;

nLines += 1;

int width = 0;

/* String buf1=null;

try{

buf1 = new String(null, "utf-8");

}

catch(Exception e){}*/

StringBuffer buf = new StringBuffer();

Tag firstTag = null;

Tag lastTag = null;

char ch = '\0';

do {

ch = getChar(pos);

while (tag != tags && tag.end <= pos)

{

tag = tag.next;

}

boolean isLink = false;

if (pos >= tag.beg && pos < tag.end)

{

isLink = false;

if (firstTag == null) {

if (currTag.end <= currPagePos)

{

currTag = tag;

}

firstTag = lastTag = tag;

tag.pos = buf.length() - pos + tag.beg;

}

else if (tag.beg == pos)

{

lastTag = tag;

tag.pos = buf.length();

}

}

pos += 1;

if (ch == ' ')

{

int j = pos-1;

while (--j >= 0 && text[j] == ' ');

if ((j < 0 || text[j] == '\r') && pos-j-1 <= INDENTATION)

{

buf.append(ch);

width += spaceWidth;

continue;

}

else if (j+2 < pos)

{

continue;

}

}

else if (ch == '\r')

{

ch = '\n';

if (pos < textLength && (text[pos] == ' ' || text[pos] == '\r'))

{

break;

}

ch = ' ';

}

else if (ch == '\0')

{

eof = true;

break;

}

else if (ch == '\n')

{

break;

}

buf.append(ch);

width += font.charWidth(ch);

}

while (width <= screenWidth);

//System.out.println("String='" + buf.toString() + "', width=" + width);

if (width > screenWidth)

{

if (ch == ' ' || ch == '\n')

{

buf.deleteCharAt(buf.length()-1);

width -= spaceWidth;

if (firstTag == null)

{

insertSpaces(buf, (screenWidth - width) / spaceWidth);

}

}

else

{

int n = buf.length();

if (!eof) {

int p = pos;

while ((ch = getChar(p++)) > ' ')

{

buf.append(ch);

}

}

pos -= splitWord(buf, n, width, firstTag == null);

}

}

String str = buf.toString();

int strLen = str.length();

g.setColor(FOREGROUND_COLOR);

g.setFont(font);

g.drawString(str, 0, i*fontHeight, Graphics.LEFT|Graphics.TOP);

if (firstTag != null) {

if (firstPageTag == null)

{

firstPageTag = firstTag;

}

g.setFont(linkFont);

lastTag = lastTag.next;

do {

int tagLen = firstTag.end - firstTag.beg;

int tagPos = firstTag.pos;

if (tagPos < 0)

{

tagLen += tagPos;

tagPos = 0;

}

if (tagPos < strLen) {

if (tagPos + tagLen > strLen)

{

tagLen = strLen - tagPos;

}

if (firstTag == currTag) {

g.setColor(CURRENT_LINK_BACKGROUND_COLOR);

g.fillRect(font.substringWidth(str, 0, tagPos), i*fontHeight,

font.substringWidth(str, tagPos, tagLen), fontHeight);

g.setColor(CURRENT_LINK_FOREGROUND_COLOR);

containsCurrTag = true;

}

else

{

g.setColor(firstTag.url.startsWith("#") || browser.historyHash.get(firstTag.url) != null

? VISITED_LINK_COLOR : LINK_COLOR);

}

// System.out.println("tagPos=" + tagPos + ", tagLen=" + tagLen + ", firstTag.beg=" + firstTag.beg + ", firstTag.end=" + firstTag.end + ", firstTag.pos=" + firstTag.pos + ", strLen=" + strLen);

g.drawSubstring(str, tagPos, tagLen, font.substringWidth(str, 0, tagPos),

i*fontHeight, Graphics.LEFT|Graphics.TOP);

}

}

while ((firstTag = firstTag.next) != lastTag);

}

}

currPageEnd = pos;

linePos[nLines] = pos;

if (!containsCurrTag && firstPageTag != null)

{

containsCurrTag = true;

currTag = firstPageTag;

continue;

}

break;

}

if (textLength != 0)

{

int sliderLength = (currPageEnd - currPagePos)*screenHeight/textLength;

if (sliderLength < MIN_SLIDER_LENGTH) {

sliderLength = MIN_SLIDER_LENGTH;

}

int sliderPos = currPagePos*screenHeight/textLength;

g.setColor(SLIDER_COLOR);

g.drawLine(screenWidth, 0, screenWidth, screenHeight);

g.setColor(SLIDER_POS_COLOR);

g.drawLine(screenWidth, sliderPos, screenWidth, sliderPos+sliderLength);

}

}

أنا بعد ما كتبت الكود و شفت اقتراح حضرتك اكتشفت أنو بدي أضطر أهدم الكود كلو لحتى عرف سترنغ و أعطيها نمط التشفير كوسيط قبل كل سترنغ بفر و هاد معقد كتير

و المشكلة أنو على الكود الحالي عم يجيب الصفحات بتشفير western 8859-1وهو طبعا غير مفهوم أبدا

شكرا كتير الك أخي الكريم

طبعاً بس حطيتلك أجزاء من الصفوف و ليس الصف كاملاً

شكراً

#7

عفوا و لكنني لن اقرأ كل هذا

ارجوك ضع فقط ما يهم :) او الذي فيه المشكلة

#8

و الله ما بعرف شو بدي قلك بس أنا ما بعرف وين المشكلة بالضبط

يمكن لازم أرجع عرف سترنغ قبل كل بفر سترنغ و أعطيها وسيط UTF-8و بيتصور أن هاد صعب كتير بس رح حاول أعمل هالشي

بصراحة أنا ماني حرّيفة جافا كتير ما بعرف حددلك بالضبط و ين المشكلة بس بتصور أنو المشكلة متل ما قلتلك

و شكرا إلك ع كل الأحوال

#9
Mohammed Mshari Ersan كتب:
عفوا و لكنني لن اقرأ كل هذا

ارجوك ضع فقط ما يهم :) او الذي فيه المشكلة

السلام عليكم ورحمته الله

الException الذي يحدث في هذا الكود

هو فيي السطر

	 String str=new String(b,"utf-8");

وإذا أردت الكود ككل هو في المرفقات

Fetch_a_page_using_a_StreamConnection_hjgjkgj.rar

banner1.gif

مواضيع مشابهة