使用getReponseCode()中止;

时间:2017-11-09 21:06:13

标签: java android json httpurlconnection

将运行以下脚本,但不会提供任何数据。在清单中,输入了Internet和网络状态的许可。

调试时,他将方法getFromServer()留在行

final int responseCode = httpURLConnection.getResponseCode ();

没有错误消息。你能帮我找到我的错吗? URL正确,可以通过浏览器访问。

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DoIt doIt = new DoIt();
        doIt.execute("Example");
    }

    public class DoIt extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... strings) {
            String url = "http://example.com/example.php";
            String response = "";

            try {
                response = new GetData().getFromServer(url);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return response;
        }

        @Override
        protected void onPostExecute(String s) {
            //super.onPostExecute(s);
            Log.d(TAG, "onPostExecute(): " + s);
        }
    }

    public class GetData {
        public String getFromServer(String url) throws IOException {
            StringBuilder sb = new StringBuilder();
            Log.d(TAG, "URL: " + url);
            URL _url = new URL(url);

            HttpURLConnection httpURLConnection = (HttpURLConnection) _url.openConnection();

            final int responseCode = httpURLConnection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                String line;

                while ((line = bufferedReader.readLine()) != null) {
                    sb.append(line);
                }

                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    Log.e(TAG, "getFromServer()", e);
                }
            }

            httpURLConnection.disconnect();

            return sb.toString();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您很可能获得404状态代码而您的StringBuilder没有任何数据。这是我从卷曲中得到的东西

curl -I http://example.com/example.php HTTP/1.1 404 Not Found Accept-Ranges: bytes Cache-Control: max-age=604800 Content-Type: text/html Date: Thu, 09 Nov 2017 22:24:01 GMT Expires: Thu, 16 Nov 2017 22:24:01 GMT Last-Modified: Tue, 07 Nov 2017 13:09:40 GMT Server: ECS (oxr/83B4) Vary: Accept-Encoding X-Cache: 404-HIT Content-Length: 1270

答案 1 :(得分:0)

网址是一个示例网址,这就是为什么它说“example.com”。对于我不想在此发布的网址,请回复以下内容:

HTTP/1.1 200 OK
Date: Fri, 10 Nov 2017 07:12:18 GMT
Server: Apache/2.4.27
X-Powered-By: PHP/5.6.30
Upgrade: h2c
Connection: Upgrade
Content-Type: application/json;charset=utf-8