为什么第二个http请求始终为空?

时间:2017-12-14 01:06:14

标签: java android

当我在PoolData()中执行http请求时,请求返回数据,而AccoutData中的请求始终为null。 在调试器中,如果我在PoolData()之后等待1-2秒,则下一个请求 - 返回非空。方法oder不能满足。为什么会这样,我怎么能等待第二次请求?

public void onClick(View v) {
        new PoolData().execute();
}
private class PoolData  extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dataPool.clear();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            PoolData();
            AccoutData();
            return null;
        }
    private void PoolData(){
        HttpHandler sh = new HttpHandler();

        String statsBrowser = "http://api.electroneum.space/v1/stats/nonBrowser";
        String browserStr = sh.makeServiceCall(statsBrowser);
        //parsing...
        }
    private void AccoutData(){
        HttpHandler sh = new HttpHandler();

        String statsAddress = "http://api.electroneum.space/v1/stats/address/" + settings.getString(lp, "n/a");     
        String addressStr = sh.makeServiceCall(statsAddress);
       //parsing...
}
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();

public HttpHandler() {
}

public String makeServiceCall(String reqUrl) {
    String response = null;
    try {
        URL url = new URL(reqUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        InputStream in = new BufferedInputStream(conn.getInputStream());
        response = convertStreamToString(in);
        Log.e(TAG, "Responce: " + response);
    } catch (MalformedURLException e) {
        Log.e(TAG, "MalformedURLException: " + e.toString());
    } catch (ProtocolException e) {
        Log.e(TAG, "ProtocolException: " + e.toString());
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e.toString());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.toString());
    }
    return response;
}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}

}

12-14 14:27:18.941 6624-6651/dev.flair.the.electroneumspacepool 

I/OpenGLRenderer: Initialized EGL, version 1.4
12-14 14:27:18.956 6624-6651/dev.flair.the.electroneumspacepool I/OpenGLRenderer: Get enable program binary service property (1)
12-14 14:27:18.956 6624-6651/dev.flair.the.electroneumspacepool I/OpenGLRenderer: Initializing program atlas...
12-14 14:27:18.958 6624-6651/dev.flair.the.electroneumspacepool I/OpenGLRenderer: Program binary detail: Binary length is 169916, program map length is 152.
12-14 14:27:18.958 6624-6651/dev.flair.the.electroneumspacepool I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 54, and path is /dev/ashmem .
12-14 14:27:18.958 6624-6651/dev.flair.the.electroneumspacepool I/OpenGLRenderer: No need to use file discriptor anymore, close fd(54).
12-14 14:27:18.979 6624-6624/dev.flair.the.electroneumspacepool W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
12-14 14:27:19.035 6624-6651/dev.flair.the.electroneumspacepool W/MALI: glDrawArrays:714: [MALI] glDrawArrays takes more than 5ms here. Total elapse time(us): 10394
12-14 14:27:23.448 6624-6672/dev.flair.the.electroneumspacepool I/System.out: url:http://api.electroneum.space/v1/stats/address/hVDo4adTwWsPK1
12-14 14:27:23.452 6624-6672/dev.flair.the.electroneumspacepool I/System.out: open:http://api.electroneum.space/v1/stats/address/hVDo4adTwWsPK1
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: [getaddrinfo]: mtk hostname=api.electroneum.space; servname=(null); cache_mode=(null), netid=0; mark=0
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: getaddrinfo( app_uid:10085
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: getaddrinfo() uid prop:
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: getaddrinfo() getuid():10085
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: [getaddrinfo]: mtk ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: [getaddrinfo]: mtk hostname=api.electroneum.space; servname=(null); cache_mode=(null), netid=0; mark=0
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: getaddrinfo( app_uid:10085
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: getaddrinfo() uid prop:
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: getaddrinfo() getuid():10085
12-14 14:27:23.454 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: [getaddrinfo]: mtk ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
12-14 14:27:23.457 6624-6672/dev.flair.the.electroneumspacepool D/libc-netbsd: getaddrinfo: api.electroneum.space get result from proxy >>
12-14 14:27:23.457 6624-6672/dev.flair.the.electroneumspacepool I/System.out: propertyValue:true
12-14 14:27:23.459 6624-6672/dev.flair.the.electroneumspacepool I/System.out: [CDS]connect[api.electroneum.space/104.27.156.123:80] tm:90
12-14 14:27:23.497 6624-6672/dev.flair.the.electroneumspacepool I/System.out: [OkHttp] sendRequest>>
12-14 14:27:23.497 6624-6672/dev.flair.the.electroneumspacepool I/System.out: [OkHttp] sendRequest<<
12-14 14:27:23.612 6624-6672/dev.flair.the.electroneumspacepool E/HttpHandler: MalformedURLException: java.io.BufferedInputStream@3d6a54f5
12-14 14:27:23.613 6624-6672/dev.flair.the.electroneumspacepool I/NetworkManagementSocketTagger: untagSocket(56)
12-14 17:08:49.897 19931-20613/dev.flair.the.electroneumspacepool E/HttpHandler: Responce:
                                                                               {"stats":{"data...."}}
12-14 17:08:49.899 19931-20613/dev.flair.the.electroneumspacepool I/System.out: url:http://api.electroneum.space/v1/stats/nonBrowser
12-14 17:08:49.900 19931-20613/dev.flair.the.electroneumspacepool I/System.out: open:http://api.electroneum.space/v1/stats/nonBrowser
12-14 17:08:49.901 19931-20613/dev.flair.the.electroneumspacepool I/System.out: [OkHttp] sendRequest>>
12-14 17:08:49.901 19931-20613/dev.flair.the.electroneumspacepool I/System.out: [OkHttp] sendRequest<<
12-14 17:08:50.008 19931-20613/dev.flair.the.electroneumspacepool E/HttpHandler: IOException: java.io.FileNotFoundException: http://api.electroneum.space/v1/stats/nonBrowser
12-14 17:08:50.008 19931-20613/dev.flair.the.electroneumspacepool E/MainActivity: Response from browser: null

0 个答案:

没有答案