从 URL 接收时出现问题

发布于 2024-10-21 10:22:55 字数 1590 浏览 3 评论 0原文

您好,我正在从 Android 设备中的 URL 接收 JSON 文件。我不知道为什么我只得到 JSON 的第一个字符,这是一个“{”而不是 {"status":"OK","num_results":5,"results":[{"id":1,"title":"校园公寓", 然后 is.available() 始终返回 1。 有趣的是,如果我在另一个设备中使用它,它会收到所有内容。

我正在使用 wifi 来连接。

我得到的输出是:

03-11 13:12:17.377: VERBOSE/Mixare(15816): attemps: 1

03-11 13:12:17.382: VERBOSE/Mixare(15816): is.available(): 1

03-11 13:12:17.462:详细/混合(15816):尝试:1

03-11 13:12:17.482:详细/混合(15816):is.available():1

03-11 13:12: 19.417:详细/混合(15816):尝试:2

03-11 13:12:19.417:详细/混合(15816):is.available():1 ...

这是代码:

    public InputStream getHttpGETInputStream(String urlStr, int attemps) throws Exception {     
    URL u = new URL(urlStr);
    InputStream is = null;
    try{            
        URLConnection uc = u.openConnection();          
        is =  uc.getInputStream();          
    }
    catch(Exception e){
        Log.v(MixView.TAG, "Excepcion leyendo inputStream: "+e.getMessage());           
    }
    Log.v(MixView.TAG, "attemps: "+attemps);
    Log.v(MixView.TAG, "is.available(): "+is.available());
    if(is.available() < 50 && attemps < 6){
        try {               
            Thread.sleep(2000);//para y lo vuelve a intentar descargar              
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
        attemps++;      
        is.close();
        return getHttpGETInputStream(urlStr,attemps);
    }       
    return is;
}

Hello I am receiving a JSON file from an URL in an Android device. I have no idea why I am getting just the first character of the JSON, this is a "{" instead of
{"status":"OK","num_results":5,"results":[{"id":1,"title":"Apartamentos Campus",
Then is.available() is returning 1 all the time.
The funny thing is that if I use in another device it receives everything.

I am using the wifi to connect.

And the output that I am getting is:

03-11 13:12:17.377: VERBOSE/Mixare(15816): attemps: 1

03-11 13:12:17.382: VERBOSE/Mixare(15816): is.available(): 1

03-11 13:12:17.462: VERBOSE/Mixare(15816): attemps: 1

03-11 13:12:17.482: VERBOSE/Mixare(15816): is.available(): 1

03-11 13:12:19.417: VERBOSE/Mixare(15816): attemps: 2

03-11 13:12:19.417: VERBOSE/Mixare(15816): is.available(): 1
...

And this is the code:

    public InputStream getHttpGETInputStream(String urlStr, int attemps) throws Exception {     
    URL u = new URL(urlStr);
    InputStream is = null;
    try{            
        URLConnection uc = u.openConnection();          
        is =  uc.getInputStream();          
    }
    catch(Exception e){
        Log.v(MixView.TAG, "Excepcion leyendo inputStream: "+e.getMessage());           
    }
    Log.v(MixView.TAG, "attemps: "+attemps);
    Log.v(MixView.TAG, "is.available(): "+is.available());
    if(is.available() < 50 && attemps < 6){
        try {               
            Thread.sleep(2000);//para y lo vuelve a intentar descargar              
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
        attemps++;      
        is.close();
        return getHttpGETInputStream(urlStr,attemps);
    }       
    return is;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花之痕靓丽 2024-10-28 10:22:55

我猜,您以错误的方式阅读内容。请使用以下代码:

public static final byte[] getBytes(URLConnection conn ) throws IOException {
                final InputStream inputStream = conn.getInputStream();      
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();         
                final byte[] b = new byte[256];
                int rv = 0;
                while ( ( rv = inputStream.read(b) ) != -1 ) baos.write(b, 0, rv);
                try  { inputStream.close(); } catch ( Exception e ) {};
            try  {
                final HttpURLConnection httpConn = (HttpURLConnection)conn;                     
                        httpConn.disconnect();
            }
            catch ( Exception e ) {};       
            final byte[] bytes =  baos.toByteArray();
            baos.close();
            return bytes;
        }

        public static final String getResponse(URLConnection conn) throws IOException {
                final byte[] bytes = getBytes(conn);
            final String response = new String(bytes,"UTF-8");
            return response;
        }      

...
final JSONObject responseObj = new JSONObject(getResponse(your_url_connection));
...

I guess, You are reading the content in wrong way. Please use following code:

public static final byte[] getBytes(URLConnection conn ) throws IOException {
                final InputStream inputStream = conn.getInputStream();      
                final ByteArrayOutputStream baos = new ByteArrayOutputStream();         
                final byte[] b = new byte[256];
                int rv = 0;
                while ( ( rv = inputStream.read(b) ) != -1 ) baos.write(b, 0, rv);
                try  { inputStream.close(); } catch ( Exception e ) {};
            try  {
                final HttpURLConnection httpConn = (HttpURLConnection)conn;                     
                        httpConn.disconnect();
            }
            catch ( Exception e ) {};       
            final byte[] bytes =  baos.toByteArray();
            baos.close();
            return bytes;
        }

        public static final String getResponse(URLConnection conn) throws IOException {
                final byte[] bytes = getBytes(conn);
            final String response = new String(bytes,"UTF-8");
            return response;
        }      

...
final JSONObject responseObj = new JSONObject(getResponse(your_url_connection));
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文