2017-08-17 25 views
0

私は奇妙な状況があります。電話や仮想デバイスを使用しているときは、すべてのリクエストがうまく動作しますが、タブレットを使用しようとすると何も動作しません。Tabletを使用してurlConnection.getInputStream()を取得できません

マイリクエスト:

String path = "https://..."; 
HttpURLConnection urlConnection = null; 
try{ 
    URL url = new URL(path); 
    urlConnection = (HttpURLConnection) url 
      .openConnection(); 
    urlConnection.setRequestMethod("GET"); 
    urlConnection = Tools.setHeader(urlConnection); 
    urlConnection.setDoInput(true); 

    LOG.debug("Start input stream"); 
    InputStream inputStream = urlConnection.getInputStream(); 
    LOG.debug("Input stream exist "+inputStream.toString()); 

    String response = Tools.streamToString(inputStream); 
    inputStream.close(); 
    JSONObject jsonResponse = new JSONObject(response); 

私はすべてのエラーメッセージを得ることはありません。プロキシサーバーを使用して、私の要求が送信され、応答が受信されたことがわかります。しかし、プログラムではurlConnection.getInputStream()メソッドは何も得られません。プログラムはそれ以上進まない。 2番目のログは表示されません。

タイムアウトを設定しようとしましたが、これは役に立ちませんでした。応答はすぐに(1-2秒)来ます。

答えて

0

タブレットAndroidのバージョンを確認してください。問題が発生している可能性があります。アンドロイドOからは、httpリクエストをサポートしていません。

解決方法:別のタブレットイメージをmarshmallowバージョンでダウンロードしてチェックします。

更新日:私は携帯電話やタブレットの両方で私の次のコードを実行し、このコード下さい

HttpURLConnection connection = null; 
BufferedReader reader = null; 
    try { 
     URL url = new URL(params[0]); 
     connection = (HttpURLConnection)url.openConnection(); 
     connection.connect(); 
     InputStream stream =connection.getInputStream(); 
     reader = new BufferedReader(new InputStreamReader(stream)); 
     StringBuffer buffer = newStringBuffer(); 
      String line = ""; 
      while ((line = reader.readLine()) != null) { 
      buffer.append(line+"\n"); 
      Log.d("Response: ", "> " + line); //here u ll get whole response...... :-) 
     } 
     return buffer.toString(); 
    } 
    catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    finally { 
     if (connection != null) { 
      connection.disconnect(); 
     } 
     try { 
      if (reader != null) { 
       reader.close(); 
       } 
      } 
      catch (IOException e) 
     { 
       e.printStackTrace(); 
     } 
    } 
    return null; 
} 
+0

としての私の画面です。 1 – Delphian

+0

あなたはyあなたが使用している "URL"は、私たちが確認できるように –

+0

私は申し訳ありません。しかし、私は別のURLをテストし、同じ結果をキャッチしました。 – Delphian

1

Heyyを試してみてください。私はあなたに私のコードを提供しています。

コード:

public class URLConnectionCheck extends Activity { 

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

    resultText = (TextView)findViewById(R.id.resultTextView); 

    new AsynchTaskTest().execute(); 

} 

private class AsynchTaskTest extends AsyncTask<Void,Void,String> 
{ 
    @Override 
    protected String doInBackground(Void... params) { 

     String path = "http://echo.jsontest.com/key/value/one/two"; 

     HttpURLConnection urlConnection = null; 
     BufferedReader reader=null; 

     try{ 
      URL url = new URL(path); 

      urlConnection = (HttpURLConnection)url.openConnection(); 

      urlConnection.connect(); 

      InputStream inputStream = urlConnection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(inputStream)); 

      StringBuffer buffer = new StringBuffer(); 
      String line = ""; 

      while((line = reader.readLine()) !=null) { 
       buffer.append(line+"\n"); 

      } 

      return buffer.toString(); 

     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      return "exception"; 
     } 
    } 

    @Override 
    protected void onPostExecute(String aVoid) { 
     super.onPostExecute(aVoid); 

     resultText.setText(aVoid); 
    } 
} 

}

、結果は私が本当のタブレット、アンドロイドバージョン5.1を使用しています::

mobile screenshot

tablet screenshot

+0

Ankit、これは要求に関連していないため、要求が動作する場合もありませんし、動作しない場合もあります。明らかに、これはタブレットの設定やフォアグラウンドプロセスに関連する問題の一種です。現時点では、私は何も変えなかったが、すべてがうまくいったが、別の時間には動作しなくなった。私は前景モードで長いプロセスを持っています。解決策を見つけようと努力してくれてありがとう、私はあなたにプラスを加えました! – Delphian

関連する問題