如何从 URL 获取字符串

发布于 2024-12-04 23:51:42 字数 848 浏览 1 评论 0原文

我的应用程序从 PHP Web 服务器获取大部分数据。

我的问题是当服务器返回错误时。 错误不是 HTML 页面,而是简单的字符串。 例如:

ERROR001

可以代表无效名称。

这是我的代码:

String responseBody = null;
URL url = new URL(strUrl);
URLConnection connection;
connection = url.openConnection();
connection.setUseCaches(false);
InputStream isResponse = (InputStream) connection.getContent(); // on errors I get IOException here
responseBody = convertStreamToString (isResponse);

当我使用它时,我在 connection.getContent(); 上收到 IOException;

我也尝试过:

HttpGet getMethod = new HttpGet(url);
String responseBody = null;
responseBody = mClient.execute(getMethod,mResponseHandler); // on errors I getClientProtocolException

但我在 mClient.execute 上得到 getClientProtocolException

有什么想法如何将 ERROR001 这样的结果读取到字符串中吗?

My application gets most of its data from a php web server.

My problem is when the server return errors.
The errors are not HTML pages, but simple strings.
for example:

ERROR001

could stand for invalid name.

here is my code:

String responseBody = null;
URL url = new URL(strUrl);
URLConnection connection;
connection = url.openConnection();
connection.setUseCaches(false);
InputStream isResponse = (InputStream) connection.getContent(); // on errors I get IOException here
responseBody = convertStreamToString (isResponse);

When I use it, I get IOException on connection.getContent();

I also tried:

HttpGet getMethod = new HttpGet(url);
String responseBody = null;
responseBody = mClient.execute(getMethod,mResponseHandler); // on errors I getClientProtocolException

but I get getClientProtocolException on mClient.execute

Any ideas how I could read a result like ERROR001 into a string?

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

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

发布评论

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

评论(3

柠栀 2024-12-11 23:51:42

问题是,出现错误时,Http 标头为 HTTP 错误 500(内部服务器错误)。
为了读取错误内容,我使用了以下代码:

    String responseBody = null;
    URL url = new URL(strUrl);
    HttpURLConnection connection;
    connection = (HttpURLConnection) url.openConnection();
    connection.setUseCaches(false);
    InputStream isResponse = null;
    try {
        isResponse = connection.getInputStream();
    } catch (IOException e) {
        isResponse = connection.getErrorStream();
    }
    responseBody = convertStreamToString (isResponse);

    return responseBody;

The problem is that on error, the Http header is HTTP Error 500 (Internal server error).
To read the error content I used the following code:

    String responseBody = null;
    URL url = new URL(strUrl);
    HttpURLConnection connection;
    connection = (HttpURLConnection) url.openConnection();
    connection.setUseCaches(false);
    InputStream isResponse = null;
    try {
        isResponse = connection.getInputStream();
    } catch (IOException e) {
        isResponse = connection.getErrorStream();
    }
    responseBody = convertStreamToString (isResponse);

    return responseBody;
温折酒 2024-12-11 23:51:42
url = new URL(desiredUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

试试这个。
我认为你没有调用connection.connect()方法。

url = new URL(desiredUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

Try this one.
I think you are not calling the connection.connect() method.

深白境迁sunset 2024-12-11 23:51:42

使用 HttpClient 时会发生什么?
你可能想尝试这个:

String responseBody;

HttpGet request = new HttpGet("your url");
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(request);
HttpEntity entity = httpResponse.getEntity();

if(entity != null){
    responseBody = convertStreamToString (entity.getContent());
}

What happens when you use HttpClien?
you may want to try this:

String responseBody;

HttpGet request = new HttpGet("your url");
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(request);
HttpEntity entity = httpResponse.getEntity();

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