Skip to content

Commit a36b3c2

Browse files
committed
JSHttpClient fix for async
1 parent 62e66fb commit a36b3c2

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

sources/net.sf.j2s.java.core/src/javajs/http/JSHttpClient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Map.Entry;
1818
import java.util.function.BiConsumer;
1919
import java.util.function.Consumer;
20+
import java.util.function.Function;
2021

2122
/**
2223
* SwingJS implementation of javajs.http.HttpClient and associated classes.
@@ -98,7 +99,7 @@ protected AjaxURLConnection(URL u) {
9899

99100
public abstract void addFormData(String name, Object value, String contentType, String fileName);
100101

101-
public abstract void getBytesAsync(Consumer<byte[]> whenDone);
102+
public abstract void getBytesAsync(Function<byte[], Void> whenDone);
102103

103104
}
104105

@@ -357,11 +358,12 @@ Response getResponse() throws IOException {
357358
public void run() {
358359
// asynchronous methods cannot throw an exception.
359360
if (allowInputStream) {
360-
conn.getBytesAsync(new Consumer<byte[]>() {
361+
conn.getBytesAsync(new Function<byte[], Void>() {
361362

362363
@Override
363-
public void accept(byte[] t) {
364+
public Void apply(byte[] t) {
364365
doCallback(t != null);
366+
return null;
365367
}
366368

367369
});

sources/net.sf.j2s.java.core/src/test/Test_HTTP.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,24 @@ public static void main(String[] args) {
4141
System.out.println(resp.getText());
4242

4343

44-
// req = client.put(new URI("https://www.compbio.dundee.ac.uk/slivka/api/services/example"));
45-
//
46-
// req.addFile("input-file",new ByteArrayInputStream(json.getBytes()));
47-
// req.addParameter("content", "len:11 long");
48-
//
49-
// resp = req.executeAsync(new Consumer<HttpResponse>() {
50-
//
51-
// @Override
52-
// public void accept(HttpResponse t) {
53-
// try {
54-
// System.out.println(t.getText());
55-
// } catch (IOException e) {
56-
// // TODO Auto-generated catch block
57-
// e.printStackTrace();
58-
// }
59-
// }
60-
//
61-
// }, null, null);
44+
req = client.put(new URI("https://www.compbio.dundee.ac.uk/slivka/api/services/example"));
45+
46+
req.addFile("input-file",new ByteArrayInputStream(json.getBytes()));
47+
req.addParameter("content", "len:11 long");
48+
49+
resp = req.executeAsync(new Consumer<HttpResponse>() {
50+
51+
@Override
52+
public void accept(HttpResponse t) {
53+
try {
54+
System.out.println(t.getText());
55+
} catch (IOException e) {
56+
// TODO Auto-generated catch block
57+
e.printStackTrace();
58+
}
59+
}
60+
61+
}, null, null);
6262

6363

6464
} catch (Exception e3) {

0 commit comments

Comments
 (0)