Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BufferedReader readLine返回奇怪的字符_Java_Android_Bufferedreader - Fatal编程技术网

Java BufferedReader readLine返回奇怪的字符

Java BufferedReader readLine返回奇怪的字符,java,android,bufferedreader,Java,Android,Bufferedreader,我试图从URL读取JSON数据,我得到了一个非常奇怪的行为。 我有一个可扩展的列表视图,每个项目都有一个按钮来恢复在线数据。 第一次单击按钮时,它工作正常,但随后的任何尝试都会失败。 在这些尝试中,readLine方法返回奇怪的字符: ��������������Y�o�F�W一定要打电话 con.disconnect(); 在末尾的finally块中关闭连接,否则将导致大量未使用的连接打开。这可能会引起问题 还有一点 con.setRequestMethod("GET"); con.setR

我试图从URL读取JSON数据,我得到了一个非常奇怪的行为。 我有一个可扩展的列表视图,每个项目都有一个按钮来恢复在线数据。 第一次单击按钮时,它工作正常,但随后的任何尝试都会失败。 在这些尝试中,readLine方法返回奇怪的字符:

��������������Y�o�F�W一定要打电话

con.disconnect();
在末尾的
finally
块中关闭连接,否则将导致大量未使用的连接打开。这可能会引起问题

还有一点

con.setRequestMethod("GET");
con.setRequestProperty("Accept-Charset", "UTF-8"); 

不会出错的


您还应该检查conn.getResponseCode()的值,以查看服务器是否返回错误。任何介于
200
299
之间的代码都是成功的代码。

我使用Apache的EntityUtils重写了我的代码,现在它开始工作了:

    static String response = null;

    public static String readContents(String url) {
      try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
        HttpGet httpGet = new HttpGet(url);
        httpResponse = httpClient.execute(httpGet);
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
      } catch (IOException e) {
        Log.d("READ FAILED", e.toString());
        return null;
      }
      return response;
    }

谢谢,我添加了断开连接,但是运气不好。我得到了一个200码。@droidparanoico我已经用你可能应该做的另外两件事更新了答案。您是否考虑过将返回的字符串回显到logcat?这将帮助您区分HTTP检索问题和返回字符串的显示问题。相同的负面结果。这不是显示问题,因为返回的数据被解析为JSONArray,当它返回这些奇怪的字符时,我得到错误:“org.json.JSONException:Value��������������怀俄明州�8.�+�美分��eYq��(类型为java.lang.String无法转换为JSONArray)“无论如何,我刚刚将字符串回显到logcat并确认了它。您可以将原始字符串数据作为字节发布吗?类似于
Arrays.toString(thestring.getBytes())
应该可以。当然,你看,我注意到了一些重要的事情,我使用了字节/字符转换器,数据似乎是正确的!我不明白。。。
con.setRequestProperty("Accept", "application/json");
    static String response = null;

    public static String readContents(String url) {
      try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
        HttpGet httpGet = new HttpGet(url);
        httpResponse = httpClient.execute(httpGet);
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
      } catch (IOException e) {
        Log.d("READ FAILED", e.toString());
        return null;
      }
      return response;
    }