如何使用Java发送GET请求

时间:2017-02-03 20:04:15

标签: java android

我正在尝试在我的Android应用程序中发送GET请求,但是在我使用调试器检查时,它发出了一些SSL错误。 给出的是我用于通过HTTP GET登录的代码

private class AsyncLogin extends AsyncTask<String, String, String>
{
    ProgressDialog pdLoading = new ProgressDialog(LoginActivity.this);
    HttpsURLConnection conn;
    URL url = null;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    }
    @Override
    protected String doInBackground(String... params) {
        try {
            url = new URL(params[0]);

        } catch (MalformedURLException e) {

            e.printStackTrace();
            return "exception";
        }
        try {
            conn = (HttpsURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Origin","https://myserver.com");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.connect();

        } catch (IOException e1) {
            e1.printStackTrace();
            return "exception";
        }

        try {

            int response_code = conn.getResponseCode();
            if (response_code == HttpsURLConnection.HTTP_OK) {
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
                return(result.toString());

            }else{

                return("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return "exception";
        } finally {
            conn.disconnect();
        }


    }

    @Override
    protected void onPostExecute(String result) {
        pdLoading.dismiss();

        if(result.equalsIgnoreCase("true"))
        {
            Intent intent = new Intent(LoginActivity.this,SuccessActivity.class);
            startActivity(intent);
            LoginActivity.this.finish();

        }else if (result.equalsIgnoreCase("false")){
            Toast.makeText(getApplicationContext(), "Invalid email or password", Toast.LENGTH_LONG).show();
        } else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {

            Toast.makeText(LoginActivity.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show();

        }
    }

}

调用此函数后,我的应用程序结束。

 org.apache.harmony.security.fortress.Engine.getInstance

1 个答案:

答案 0 :(得分:1)

为什么要转换为HttpsURLConnection,正常的HttpURLConnection也应该这样做,并且可以使用HTTP和HTTPS。 接下来,我会将e.printStackTrace();替换为Log.e(TAG, "error creating HTTP connection", e);,然后您会看到会发生什么。 你能在这里发布Stacktrace吗?然后我们可以看到它是什么错误。

顺便说一句: conn.setDoInput(true); conn.setDoOutput(true);

对于简单的GET请求不是必需的。

相关问题