Android HttpUrlConnection不起作用

时间:2016-05-22 10:21:38

标签: android httpurlconnection

我使用localhost创建了网站,它有一些值和按钮。 如果有人访问了网址(例如http://myhost/?status2=0),则值会发生变化。

我想在Android中执行此操作,因此我使用了HttpURLConnection,但它无法正常工作。

如果我在Android中使用Intent(ACTION_VIEWUri.parse("http..."))访问网址,则效果很好。

但是我使用HttpURLConnection访问了网址,但它不起作用。我不知道出了什么问题。请帮帮我。

这是Android应用中的代码。我已经检查了清单。

try{
    URL url = new URL("http://myhost/?status2=0");

    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setDoInput(true);
    conn.setDoOutput(true);

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

1 个答案:

答案 0 :(得分:0)

好的,您可以使用RetrofitOkHttp,但在HttpUrlConnection尝试使用:

    public  void refreshToken(String url,String refresh,String cid,String csecret) throws IOException {
        URL refreshUrl = new URL(url + "token");
        HttpURLConnection urlConnection = (HttpURLConnection) refreshUrl.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setUseCaches(false);
        String urlParameters = "grant_type=refresh_token&client_id=" + cid + "&client_secret=" + csecret + "&refresh_token=" + refresh;
        // Send post request
        urlConnection.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();
        int responseCode = urlConnection.getResponseCode();
        //this is my Pojo class . I am converting response to Pojo with Gson
        RefreshTokenResult refreshTokenResult = new RefreshTokenResult();
        if (responseCode == 200) {

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(urlConnection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            // JSONObject obj=new JSONObject(response.toString());
            Gson gson = new Gson();
            refreshTokenResult = gson.fromJson(response.toString(), RefreshTokenResult.class);
            //handle new token ...
            mLoginPrefsEditor = mLoginPreferences.edit();
            mLoginPrefsEditor.commit();
            mLoginPrefsEditor.putString(mContext.getResources().getString(R.string.pref_accesstoken), "Bearer " + refreshTokenResult.getAccessToken());
            mLoginPrefsEditor.putString(mContext.getResources().getString(R.string.pref_refreshtoken), refreshTokenResult.getRefreshToken());
            mLoginPrefsEditor.commit();
        }   
}

在我的其他请求中,我使用OkHttp支持Retrofit,但在刷新令牌中 - 刷新令牌对我来说是一个关键过程 - 我正在使用HttpUrlConnection

当我在刷新令牌方法中使用OkHttpRetrofit时,它有时会不正确。

我得到了这个:有时你需要使用HttpUrlConnection的基本握手方法。

相关问题