Skip to content

Commit 3de2225

Browse files
author
jossonsmith
committed
Fixed bug on XMLHttpRequest's ready state value
1 parent 465b41b commit 3de2225

6 files changed

Lines changed: 65 additions & 30 deletions

File tree

sources/net.sf.j2s.ajax/.j2s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Java2Script Configuration
2-
#Thu Jun 15 00:01:04 CST 2006
2+
#Thu Jul 13 23:34:33 CST 2006
33
j2s.abandoned.resources.list=
44
j2s.compiler.abbreviation=true
55
j2s.compiler.status=enable

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/HttpRequest.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* 2006-2-11
3636
*/
37-
public final class HttpRequest implements Runnable {
37+
public final class HttpRequest {
3838
private int status;
3939
private int readyState;
4040

@@ -96,19 +96,27 @@ public void open(String method, String url, boolean async) {
9696
this.url = url;
9797
responseText = null;
9898
responseXML = null;
99+
readyState = 1;
100+
if (onreadystatechange != null) {
101+
onreadystatechange.onLoading();
102+
}
99103
}
100104
public void send() {
101105
send(null);
102106
}
103107
public void send(String str) {
104108
content = str;
105109
if (asynchronous) {
106-
new Thread(this).start();
110+
new Thread(new Runnable() {
111+
public void run() {
112+
request();
113+
}
114+
}).start();
107115
} else {
108-
run();
116+
request();
109117
}
110118
}
111-
public void run() {
119+
private void request() {
112120
try {
113121
connection = (HttpURLConnection) new URL(url).openConnection();
114122
connection.setDoInput(true);
@@ -125,9 +133,6 @@ public void run() {
125133
connection.setRequestProperty(key, (String) headers.get(key));
126134
}
127135
connection.setUseCaches(false);
128-
if (onreadystatechange != null) {
129-
onreadystatechange.onLoading();
130-
}
131136
if ("post".equalsIgnoreCase(method)) {
132137
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
133138
if (content != null) {
@@ -137,39 +142,51 @@ public void run() {
137142
dos.close();
138143
}
139144
InputStream is = connection.getInputStream();
140-
if (onreadystatechange != null) {
141-
onreadystatechange.onLoading();
142-
}
143145
ByteArrayOutputStream baos = new ByteArrayOutputStream();
144146
byte[] buffer = new byte[1024];
145147
int read;
146148
while ((read = is.read(buffer)) != -1) {
149+
if (readyState < 2) {
150+
readyState = 2;
151+
if (onreadystatechange != null) {
152+
onreadystatechange.onLoaded();
153+
}
154+
}
147155
baos.write(buffer, 0, read);
148-
if (onreadystatechange != null) {
149-
onreadystatechange.onInteractive();
156+
if (readyState != 3) {
157+
readyState = 3;
158+
if (onreadystatechange != null) {
159+
onreadystatechange.onInteractive();
160+
}
150161
}
151162
}
152163
is.close();
153-
if (onreadystatechange != null) {
154-
onreadystatechange.onLoaded();
155-
}
156164
responseText = baos.toString();
157165
status = connection.getResponseCode();
166+
readyState = 4;
158167
if (onreadystatechange != null) {
159168
onreadystatechange.onComplete();
160169
}
161170
connection.disconnect();
171+
readyState = 0;
172+
/*
162173
if (onreadystatechange != null) {
163174
onreadystatechange.onUninitialized();
164175
}
176+
*/
165177
} catch (IOException e) {
166178
e.printStackTrace();
179+
readyState = 4;
167180
if (onreadystatechange != null) {
168181
onreadystatechange.onComplete();
169182
}
183+
connection = null;
184+
readyState = 0;
185+
/*
170186
if (onreadystatechange != null) {
171187
onreadystatechange.onUninitialized();
172188
}
189+
*/
173190
}
174191
}
175192

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/IXHRCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* 2006-2-11
2020
*/
2121
public interface IXHRCallback {
22-
public void onUninitialized();
22+
//public void onUninitialized();
2323
public void onLoading();
2424
public void onLoaded();
2525
public void onInteractive();

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/XHRCallbackAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public void onLoading() {
3636

3737
}
3838

39-
public void onUninitialized() {
40-
41-
}
39+
// public void onUninitialized() {
40+
//
41+
// }
4242

4343
}

sources/net.sf.j2s.ajax/ajaxswt/net/sf/j2s/ajax/XHRCallbackSWTAdapter.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
* @author josson smith
2222
*
2323
* 2006-2-11
24+
*
25+
* @j2sIgnoreImport java.lang.Runnable, org.eclipse.swt.widgets.Display
26+
*
2427
*/
2528
public class XHRCallbackSWTAdapter implements IXHRCallback {
2629

@@ -36,9 +39,12 @@ public void swtOnLoaded() {
3639
public void swtOnLoading() {
3740
}
3841

39-
public void swtOnUninitialized() {
40-
}
42+
// public void swtOnUninitialized() {
43+
// }
4144

45+
/**
46+
* @j2sNative this.swtOnComplete();
47+
*/
4248
public void onComplete() {
4349
Display.getDefault().syncExec(new Runnable() {
4450
public void run() {
@@ -47,6 +53,9 @@ public void run() {
4753
});
4854
}
4955

56+
/**
57+
* @j2sNative this.swtOnInteractive();
58+
*/
5059
public void onInteractive() {
5160
Display.getDefault().syncExec(new Runnable() {
5261
public void run() {
@@ -55,6 +64,9 @@ public void run() {
5564
});
5665
}
5766

67+
/**
68+
* @j2sNative this.swtOnLoaded();
69+
*/
5870
public void onLoaded() {
5971
Display.getDefault().syncExec(new Runnable() {
6072
public void run() {
@@ -63,6 +75,9 @@ public void run() {
6375
});
6476
}
6577

78+
/**
79+
* @j2sNative this.swtOnLoading();
80+
*/
6681
public void onLoading() {
6782
Display.getDefault().syncExec(new Runnable() {
6883
public void run() {
@@ -71,12 +86,15 @@ public void run() {
7186
});
7287
}
7388

74-
public void onUninitialized() {
75-
Display.getDefault().syncExec(new Runnable() {
76-
public void run() {
77-
swtOnUninitialized();
78-
}
79-
});
80-
}
89+
// /**
90+
// * @j2sNative this.swtOnUninitialized();
91+
// */
92+
// public void onUninitialized() {
93+
// Display.getDefault().syncExec(new Runnable() {
94+
// public void run() {
95+
// swtOnUninitialized();
96+
// }
97+
// });
98+
// }
8199

82100
}

sources/net.sf.j2s.ajax/j2sajax/HttpRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ c$ = Clazz.decorateAsClass (function () {
7373
this.send = function (str) {
7474
this.transport.send (str);
7575
};
76-
}, net.sf.j2s.ajax, "HttpRequest", null, Runnable);
76+
}, net.sf.j2s.ajax, "HttpRequest");

0 commit comments

Comments
 (0)