2017-08-17 66 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()方法中什么都没有。该计划不会进一步。我没有看到第二个日志。

我试图设置超时,但这没有帮助。反应很快(1-2秒)。

回答

0

检查您的平板电脑的Android版本,可能会导致问题。从ANDROID O开始,它不支持http请求。

解决方法是:下载与棉花糖版本的另一个平板电脑图像,并检查它。

更新:试试这个代码,请

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

我使用的真正平板电脑的Android 5.1版本。 1 – Delphian

+0

你能分享一下吗? ou正在使用的“URL”,soo,我们可以检查 –

+0

我不能抱歉。但是我测试了不同的网址并获得了相同的结果。 – 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); 
    } 
} 

}

而结果是在我的屏幕为::

mobile screenshot

tablet screenshot

+0

Ankit,这与请求无关,因为请求可能会或可能不会工作,在某些时候,我没有得到对该程序的响应,尽管我看到响应存在。显然,这是与平板电脑设置或前台进程相关的某种问题。目前,一切运作良好,虽然我没有改变任何东西,然后在另一次停止工作。在前景模式下我有一个漫长的过程。感谢您努力寻找解决方案,我为您添加了一个优点! – Delphian