Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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 如何检查InputStream是否正在发送数据?(与GSON合作)_Java_Android_Listener_Inputstream - Fatal编程技术网

Java 如何检查InputStream是否正在发送数据?(与GSON合作)

Java 如何检查InputStream是否正在发送数据?(与GSON合作),java,android,listener,inputstream,Java,Android,Listener,Inputstream,我没有在StackOverflow上找到这个问题,或者我用松散的关键字搜索 我通常在互联网上获取JSON文件的InputSteam,这是我的主要功能: static HttpGet getRequest; static HttpResponse getResponse; public static InputStream retrieveStream(String url, int timeout) { getRequest = null; getResponse =

我没有在StackOverflow上找到这个问题,或者我用松散的关键字搜索

我通常在互联网上获取JSON文件的InputSteam,这是我的主要功能:

    static HttpGet getRequest;
    static HttpResponse getResponse;
    public static InputStream retrieveStream(String url, int timeout) {

getRequest = null;
getResponse = null;

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout);
HttpConnectionParams.setSoTimeout(httpParameters, timeout);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);

getRequest = new HttpGet(url);

try {

    getResponse = client.execute(getRequest);

    final int statusCode = getResponse.getStatusLine().getStatusCode();

    if (statusCode != HttpStatus.SC_OK) {
        Log.w("ERROR", "Error " + statusCode + " for URL " + url);
        return null;
    }

    HttpEntity getResponseEntity = getResponse.getEntity();

    return getResponseEntity.getContent();

} catch (IOException e) {
    getRequest.abort();
    Log.w("ERROR", "Error for URL " + url, e);
} catch (Exception e) {
    getRequest.abort();
    Log.w("ERROR", "Error for URL " + url, e);
}

return null;
    }
为了避免出现错误或允许用户取消请求,因为Internet已打开但运行不正常,我想在我的应用程序未接收数据且未完成时设置计数器。我知道怎么做计数器,但是,我怎么能做那种类型的“监听器”

我用于继续此操作(始终异步):

提前谢谢

            InputStream source = f.retrieveStream(params[0]);
        Gson gson = new Gson();
        Reader reader = new InputStreamReader(source);

        response = gson.fromJson(reader, Usuario.class);