6

So I am trying to download and load an object from a file stored on a webserver. The code I use is inside a try-catch block in an AsyncTask:

URL url = new URL("http://www.mydomain.com/thefileIwant");
URLConnection urlConn = url.openConnection();
ObjectInputStream ois = new ObjectInputStream(urlConn.getInputStream());
foo = (Foo) ois.readObject();
ois.close();

I build the file with this code:

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("thefileIwant"));
oos.writeObject(foo);
oos.close();

When I try and read the object in the first piece of code I get an IOExecption that the UTF Data Format does not match UTF-8. I have tried re-building the file a couple of times and it always gives me the same error. Can I download an Object like this?

Flynn
  • 5,903
  • 7
  • 38
  • 55
  • What's the code on the server side? How do you generate `thefileIwant`? – kichik May 11 '12 at 18:36
  • It is just hosted via http and is completely publicly accessible. I just added the code I use the make the file – Flynn May 11 '12 at 18:41
  • Sounds like your server is sending the wrong content type. Make sure it's `application/octet-stream`. – kichik May 11 '12 at 21:17

3 Answers3

1

This looks like an encoding problem. I think kichik is right and most likely your server is sending data using the wrong content type, but I think you'll need to set it to application/x-java-serialized-object instead. Try adding the following lines right after opening the URLConnection:

urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "application/x-java-serialized-object");

If that doesn't work (your server may not be able to sent it using that type) you can either try to use Socket instead of UrlConnection, or else serialize your object using XML or JSON and get that via HttpUrlConnection

THelper
  • 15,333
  • 6
  • 64
  • 104
0

Try this. It's similar to your code but with a couple of differences. The sequential read/write may be a little antiquated but works well for me.

  URL url = new URL("your URL");
  HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
  urlConn.setRequestMethod("GET");
  urlConn.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
  urlConn.connect();
  InputStream is = urlConn.getInputStream();
  byte[] buffer = new byte[1024];
  int numRead = 0;
  FileOutputStream fos = new FileOutputStream("Your File");
  while ((numRead = is.read(buffer)) > 0) {
    fos.write(buffer, 0, numRead);
  }
  fos.close();
dda
  • 6,030
  • 2
  • 25
  • 34
0

This works for us, Using some Apache libraries

        FileOutputStream fos = new FileOutputStream("Your File");
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        HttpClient client =  new DefaultHttpClient(ccm, params);


        HttpGet httpGet = new HttpGet(downloadURL);
        try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        Log.d(LOG_TAG, "code is " + statusCode);
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ( (len1 = content.read(buffer)) > 0 ) {
                fos.write(buffer,0, len1);
         }                              

         success = true;
        } else {
            Log.e(LOG_TAG_JSON, "Failed to download file " + downloadURL);
        }
        if (null != response.getEntity())
        {
            response.getEntity().consumeContent();
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        Log.e(LOG_TAG, "downloadVersion " + e.toString());
        e.printStackTrace();
    }
Kibi
  • 1,860
  • 1
  • 29
  • 39