为什么 URL 响应为 Null?

Why is URL response Null?

我有一个简单的 class JSONParse,我想要 return 任何 URL 的响应。 我想要一个单独的 class,在那里我可以直接获得任何 URL 的响应。 我试过了,但我得到的是 Null 而不是 json 响应:(

这是我的Class

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class JSONParser extends AsyncTask<String , Void ,String> {
    private String server_response;
    Activity activity;
    private static  String response;

    public String getResponse(Activity activity, String url){
        this.activity = activity;
        this.execute(url);
        return response;
    }
    @Override
    protected String doInBackground(String... strings) {
        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();

            int responseCode = urlConnection.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                server_response = readStream(urlConnection.getInputStream());
                Log.v("CatalogClient", server_response);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        response = s;
    }

    private String readStream(InputStream in) {
        BufferedReader reader = null;
        StringBuffer response = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return response.toString();
    }
}

这是我调用此方法的地方

        String url = "https://jsonplaceholder.typicode.com/posts/1";
        String response = new JSONParser().getResponse(url);
        Log.d(TAG, "Response is :"+response);

我在此处得到 Null 响应,但在 class JSONParse 中得到正确响应。 有谁知道这里发生了什么:(

正在调用 AsyncTask:

String url = "https://jsonplaceholder.typicode.com/posts/1";
new JSONParser(url).execute();

以及整个 AsyncTask 逻辑:

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class JSONParser extends AsyncTask<String , Void ,String> {
    private String server_response;
    private static  String response;
    private String serverUrl = url;

    public void JSONParser(String url){
        this.serverUrl = url;
    }

    @Override
    protected String doInBackground(String... strings) {
        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(serverUrl);
            urlConnection = (HttpURLConnection) url.openConnection();

            int responseCode = urlConnection.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                server_response = readStream(urlConnection.getInputStream());
                Log.v("CatalogClient", server_response);
                return server_response;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        response = s;
        Log.d(TAG, "Response is :"+response);
    }

    private String readStream(InputStream in) {
        BufferedReader reader = null;
        StringBuffer response = new StringBuffer();
        try {
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return response.toString();
    }
}

您在 doInBackground 中 return 为空,这就是您的 onPostExecute 为或将为空的原因。将 return 设置为您的回复,您的回复如下:

@Override
protected String doInBackground(String... strings) {
    URL url;
    HttpURLConnection urlConnection = null;

    try {
        url = new URL(strings[0]);
        urlConnection = (HttpURLConnection) url.openConnection();

        int responseCode = urlConnection.getResponseCode();

        if(responseCode == HttpURLConnection.HTTP_OK){
            server_response = readStream(urlConnection.getInputStream());
            Log.v("CatalogClient", server_response);
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return server_response;
}