2016-05-22 3 views
0

localhostを使用してWebサイトを作成しました。いくつかの値とボタンがあります。 誰かがURLにアクセスした場合(たとえばhttp://myhost/?status2=0)、値は変更されます。Android HttpUrlConnectionが動作しません

Androidでこの操作をしたいので、HttpURLConnectionを使用しましたが動作しません。

AndroidにIntent(ACTION_VIEWUri.parse("http..."))を使用してURLにアクセスするとうまくいきます。

しかし、私はHttpURLConnectionを使用してURLにアクセスしても動作しません。私は何が間違っているのか分からない。私を助けてください。

これはAndroidアプリのコードです。私は既にマニフェストを確認しました。 [OK]を

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(); 
} 
+0

私はあなたが "HttpURLConnection"を使用したいと思っていますが、あなたのURLに "www"が含まれているはずです。それを試して、それが動作するかどうか確認してください? – pooyan

+0

OkHttpサードパーティライブラリを使用することができます。 HttpURLConnection上では多くの方が優先されます。 – Vucko

答えて

0

あなたはRetrofit使用したりOkHttpが、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(); 
     } 
} 

を私はOkHttpRetrofitを裏付け使用していますが、リフレッシュトークンに - リフレッシュトークンは重要なプロセスであります私のために - 私はHttpUrlConnectionを使用しています。

リフレッシュトークン方式でOkHttpまたはRetrofitを使用すると、時には正しく行われません。

私はこれを持っています:HttpUrlConnectionで基本的なハンドシェイク方法を使用する必要があることがあります。

関連する問題