为什么URL响应为空?

时间:2018-05-23 07:08:33

标签: android httpurlconnection

我有一个简单的类JSONParse,我希望返回任何URL的响应。 我想要一个单独的类,我可以直接获取任何URL的响应。 我已经尝试了但是我得到Null而不是json响应:(

这是我的班级

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响应,但我在JSONParse类中得到了正确的响应。 有谁知道这里发生了什么:(

2 个答案:

答案 0 :(得分:1)

调用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();
    }
}

答案 1 :(得分:0)

您在doInBackground中返回null,这就是您的onPostExecute为或将为null的原因。设置返回您的回复,在您的情况下将如下所示:

@objc func keyboardWillShow(_ notification: Notification) {
    if bottomTextField.isFirstResponder {
        view.frame.origin.y = -getKeyboardHeight(notification: notification)
    }
}

func getKeyboardHeight(notification: Notification) -> CGFloat {
    let userInfo = notification.userInfo
    let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
    return keyboardSize.cgRectValue.height
}