Android HttpUrlConnection Url在模拟器上不起作用 [英] Android HttpUrlConnection Url doesn't work on emulator

查看:268
本文介绍了Android HttpUrlConnection Url在模拟器上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此URL中获取字符串形式的json对象 http:// digitalcollections.tcd.ie/home/getMeta.php?pid=MS4418_021 。 downloadUrl函数出现错误后无法正常工作。

I am trying to get json object as string from this url http://digitalcollections.tcd.ie/home/getMeta.php?pid=MS4418_021. It doesn't work I get an error after downloadUrl function.

java.io.IOException: unexpected end of stream on Connection{digitalcollections.tcd.ie:80, proxy=DIRECT@ hostAddress=134.226.115.12 cipherSuite=none protocol=http/1.1} (recycle count=0)

确实适用于此androidhive网址 http://api.androidhive.info/volley/person_object.json
我是httpconnection的新手,以下是我的下载url函数。此行似乎显示错误HttpURLConnection conn =(HttpURLConnection)url.openConnection();在调试器中,该行之后的conn.getInputStream()显示IO异常和原因java.io.EOFException:\n未找到:size = 0 content = ...

Although it does work for this androidhive url http://api.androidhive.info/volley/person_object.json. I am new to httpconnection below is my download url function. Error seems to show in this line HttpURLConnection conn = (HttpURLConnection) url.openConnection(); In the debugger after that line conn.getInputStream() shows the IO exception and the cause java.io.EOFException: \n not found: size=0 content=...

 // Given a string representation of a URL, sets up a connection and gets
 // an input stream.
 private InputStream downloadUrl(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(20000 /* milliseconds */);
        conn.setConnectTimeout(30000 /* milliseconds */);
        conn.setRequestMethod("GET");
        //conn.setDoInput(true);        
        // Starts the query
        conn.connect();
        InputStream stream = conn.getInputStream();
        return stream;
    }

其他功能。

 // Uses AsyncTask to create a task away from the main UI thread. This task takes a
    // URL string and uses it to create an HttpUrlConnection. Once the connection
    // has been established, the AsyncTask downloads the contents of the webpage as
    // an InputStream. Finally, the InputStream is converted into a string, which is
    // displayed in the UI by the AsyncTask's onPostExecute method.
    private class DownloadXMLTask extends AsyncTask<String, Void, List<Entry>> {
        private String urlFront = "";
        @Override
        protected List<Entry> doInBackground(String... urls) {
            // params comes from the execute() call: params[0] is the url.
            try {
                    return loadJsonFromNetwork(urls[0]);
                } catch (IOException e) {
                    Log.d(TAG, "Unable to retrieve web page. URL may be invalid.");
                    return null;
                } catch (JSONException e) {
                    Log.d(TAG, "XMLPULLPARSER ERROR IN download json task function");
                    return null;
                }
            }

        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(List<Entry> result) {
            //post execution stuff
        }
    }

正在加载json和解析器,该解析器可能尚未工作,尚未对其进行测试。

Loading json and parser, the parser might not work haven't tested it yet.

 private List<Entry> loadJsonFromNetwork(String urlString) throws IOException, JSONException {
        InputStream stream = null;
        int len = 20000; //max amount of characters to display in string
        List<Entry> entries = new ArrayList<Entry>();

        try {
            stream = downloadUrl(urlString);  //IOException
            String jsonStr = readit(stream,len);
            if(jsonStr.equals(null)){
                Log.d(TAG, "ERROR json string returned null");
                return entries;
            }
            JSONObject jsonObj = new JSONObject(jsonStr);

            //Not sure if the json parser works yet haven't got that far 
            // Getting JSON Array node
            identifier = jsonObj.getJSONArray("identifier");

            // looping through All Contacts
            for (int i = 0; i < identifier.length(); i++) {
                JSONObject c = identifier.getJSONObject(i);

                String id = c.getString("type");
                if(id.equals("DRIS_FOLDER")) {
                    String folder = c.getString("$");
                    entries.add(new Entry(null,null,null,folder));
                }
            }

            // Makes sure that the InputStream is closed after the app is
            // finished using it.
          //This is where IOexception is called and stream is null
        } catch (IOException e) {
            Log.d(TAG, "Unable to retrieve json web page. URL may be invalid."+ e.toString());
            return entries;
        }
        finally {
            if (stream != null) {
                stream.close();
            }
        }
        return entries;
    }

我正在Nexus_5_API_23模拟器上运行它。

I am running this on a Nexus_5_API_23 emulator.

谢谢。

更新:

在Nexus_5_API_23上不起作用仿真器?虽然它可以在三星GT-ST7500外接电话上使用。

Doesn't work on Nexus_5_API_23 emulator?? Although it works on a Samsung GT-ST7500 external phone. Want it to work for the emulator.

推荐答案

问题是我的计算机上的防病毒软件/防火墙。这阻止了我的连接,这就是为什么它可以在外部电话而不是模拟器上运行的原因。我禁用了防病毒/防火墙,并且可以正常工作。这里有一个网络限制列表 http://developer.android.com/tools /devices/emulator.html#networkinglimitations

The problem was my antivirus/firewall on my computer. It was blocking my connection and that's why it was working on a external phone and not emulator. I disabled my antivirus/firewall and it worked. There is a list of network limitations here http://developer.android.com/tools/devices/emulator.html#networkinglimitations

这篇关于Android HttpUrlConnection Url在模拟器上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆