安卓:下载对象

6

我想从存储在web服务器上的文件中下载和加载一个对象。 我使用的代码在AsyncTask中的try-catch块内:

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();

我用以下代码构建文件:

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

在第一段代码中尝试读取对象时,我遇到了一个IOExecption,表示UTF数据格式与UTF-8不匹配。我已经尝试重新构建该文件几次,但总是出现相同的错误。我能下载这样的一个对象吗?


服务器端的代码是什么?你如何生成thefileIwant - kichik
它只是通过http托管,并且完全可以公开访问。我只是添加了我用来制作文件的代码。 - Flynn
似乎您的服务器正在发送错误的内容类型。请确保它是 application/octet-stream - kichik
3个回答

1

这看起来像是一个编码问题。我认为kichik是正确的,很可能你的服务器正在使用错误的内容类型发送数据,但我认为你需要将其设置为application/x-java-serialized-object。 尝试在打开URLConnection后添加以下行:

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

如果这不起作用(您的服务器可能无法使用该类型发送它),您可以尝试使用Socket而不是UrlConnection,或者使用XML或JSON序列化对象并通过HttpUrlConnection获取它。

0

试试这个。它和你的代码类似,但有一些不同之处。连续读写可能有点过时了,但对我来说效果很好。

  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

这对我们来说可行,使用了一些Apache库

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接