simple HttpURLConnection POST file multipart/form-data from android to google blobstore

use okhttp and use following snippet (taken from recipes) adjust the header values according to what your server expects. private static final String IMGUR_CLIENT_ID = “…”; private static final MediaType MEDIA_TYPE_PNG = MediaType.parse(“image/png”); private final OkHttpClient client = new OkHttpClient(); public void run() throws Exception { // Use the imgur image upload API as documented … Read more

HttpURLConnection java.io.FileNotFoundException

You can get a FileNotFoundException from HttpUrlConnection (and OkHttpClient) if your server returns >= HTTPStatus.BAD_REQUEST (400). You should check the status code first to check what stream you need to read. int status = connection.getResponseCode(); if(status >= HttpStatus.SC_BAD_REQUEST) in = connection.getErrorStream(); else in = connection.getInputStream(); HttpStatus deprecated. Latest syntax seems to be: InputStream inputStream; int … Read more

How to set Content Type on HttpURLConnection?

If you really want to use the HttpURLConnection you can use the setRequestProperty method like: myHttpURLConnection.setRequestProperty(“Content-Type”, “text/plain; charset=utf-8”); myHttpURLConnection.setRequestProperty(“Expect”, “100-continue”); However, if I were you I’d look into using the Apache HTTP libraries. They’re a little higher-level and easier to use. With them you would do it with something like: HttpGet get = new HttpGet(“http://192.168.1.36/”); … Read more

How to enable wire logging for a java HttpURLConnection traffic?

According to Sun’s HttpURLConnection source there is some logging support via JUL. Setup (adjust path as required): -Djava.util.logging.config.file=/full/path/to/logging.properties logging.properties: handlers= java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level = FINEST sun.net.www.protocol.http.HttpURLConnection.level=ALL This will log to the console, adjust as required to e.g. log to a file. Example output: 2010-08-07 00:00:31 sun.net.www.protocol.http.HttpURLConnection writeRequests FIN: sun.net.www.MessageHeader@16caf435 pairs: {GET /howto.html HTTP/1.1: null}{User-Agent: Java/1.6.0_20}{Host: www.rgagnon.com}{Accept: … Read more

Java simple code: java.net.SocketException: Unexpected end of file from server

“Unexpected end of file” implies that the remote server accepted and closed the connection without sending a response. It’s possible that the remote system is too busy to handle the request, or that there’s a network bug that randomly drops connections. It’s also possible there is a bug in the server: something in the request … Read more

Android HTTPUrlConnection : how to set post data in http body?

If you want to send String only try this way: String str = “some string goes here”; byte[] outputInBytes = str.getBytes(“UTF-8”); OutputStream os = conn.getOutputStream(); os.write( outputInBytes ); os.close(); But if you want to send as Json change Content type to: conn.setRequestProperty(“Content-Type”,”application/json”); and now our str we can write: String str = “{\”x\”: \”val1\”,\”y\”:\”val2\”}”; Hope … Read more

How to check if InputStream is Gzipped?

It’s not foolproof but it’s probably the easiest and doesn’t rely on any external data. Like all decent formats, GZip too begins with a magic number which can be quickly checked without reading the entire stream. public static InputStream decompressStream(InputStream input) { PushbackInputStream pb = new PushbackInputStream( input, 2 ); //we need a pushbackstream to … Read more

Parse JSON from HttpURLConnection object

You can get raw data using below method. BTW, this pattern is for Java 6. If you are using Java 7 or newer, please consider try-with-resources pattern. public String getJSON(String url, int timeout) { HttpURLConnection c = null; try { URL u = new URL(url); c = (HttpURLConnection) u.openConnection(); c.setRequestMethod(“GET”); c.setRequestProperty(“Content-length”, “0”); c.setUseCaches(false); c.setAllowUserInteraction(false); c.setConnectTimeout(timeout); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)