Android: Download Object

So, I am trying to upload and download an object from a file stored on a web server. The code I use is inside the try-catch block in 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 am creating a file using this code:

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

When I try to read an object in the first code snippet, I get an IOExecption that the UTF data format is not UTF-8 compatible. I tried rebuilding the file a couple of times, and it always gives me the same error. Is it possible to load such an object?

+6
source share
3 answers

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

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

If this does not work (your server will not be able to send it using this type), you can either try to use Socket instead of UrlConnection, or serialize your object using XML or JSON and get it through HttpUrlConnection

+1
source

Try it. This is similar to your code, but with a few differences. Sequential read / write may be a bit dated 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(); 
0
source

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(); } 
0
source

Source: https://habr.com/ru/post/915525/


All Articles