330 |
|
|
331 |
|
|
332 |
public static byte[] getContent(String uri, int timeout) throws IOException { |
public static byte[] getContent(String uri, int timeout) throws IOException { |
|
byte buffer[] = new byte[256]; |
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
|
|
|
|
333 |
URL url = new URL(uri); |
URL url = new URL(uri); |
334 |
URLConnection connection = url.openConnection(); |
URLConnection connection = url.openConnection(); |
335 |
HttpURLConnection c; |
|
336 |
connection.setConnectTimeout(timeout); |
connection.setConnectTimeout(timeout); |
337 |
InputStream is = connection.getInputStream(); |
InputStream is = connection.getInputStream(); |
338 |
|
|
339 |
try { |
return readInputStream(is); |
|
int count; |
|
|
while ( (count = is.read(buffer)) != -1) { |
|
|
baos.write(buffer, 0, count); |
|
|
} |
|
|
} finally { |
|
|
try { |
|
|
is.close(); |
|
|
baos.close(); |
|
|
} catch (Exception e) {} //never mind if close throws an exception |
|
|
} |
|
|
|
|
|
return baos.toByteArray(); |
|
340 |
} |
} |
341 |
|
|
342 |
public static byte[] postContent(String uri, String data, int timeout) throws IOException { |
public static byte[] postContent(String uri, String data, int timeout) throws IOException { |
|
byte buffer[] = new byte[256]; |
|
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
|
|
|
|
343 |
URL url = new URL(uri); |
URL url = new URL(uri); |
344 |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
345 |
connection.setDoOutput(true); |
connection.setDoOutput(true); |
351 |
|
|
352 |
InputStream is = connection.getInputStream(); |
InputStream is = connection.getInputStream(); |
353 |
|
|
354 |
|
return readInputStream(is); |
355 |
|
|
356 |
|
} |
357 |
|
|
358 |
|
static byte[] readInputStream(InputStream is) throws IOException{ |
359 |
|
byte buffer[] = new byte[1024]; |
360 |
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes |
361 |
try { |
try { |
362 |
int count; |
int count; |
363 |
while ( (count = is.read(buffer)) != -1) { |
while ( (count = is.read(buffer)) != -1) { |
370 |
} catch (Exception e) {} //never mind if close throws an exception |
} catch (Exception e) {} //never mind if close throws an exception |
371 |
} |
} |
372 |
|
|
373 |
return baos.toByteArray(); |
return baos.toByteArray(); |
374 |
} |
} |
375 |
|
|
376 |
OnItemSelectedListener methodSelected = new OnItemSelectedListener() { |
OnItemSelectedListener methodSelected = new OnItemSelectedListener() { |