Android Can';无法使用Tablet获取urlConnection.getInputStream()
我有一个奇怪的情况。使用电话和虚拟设备时,所有请求都可以正常工作,但如果我尝试使用平板电脑,则一切都不起作用 我的请求:Android Can';无法使用Tablet获取urlConnection.getInputStream(),android,request,httpurlconnection,tablet,Android,Request,Httpurlconnection,Tablet,我有一个奇怪的情况。使用电话和虚拟设备时,所有请求都可以正常工作,但如果我尝试使用平板电脑,则一切都不起作用 我的请求: String path = "https://..."; HttpURLConnection urlConnection = null; try{ URL url = new URL(path); urlConnection = (HttpURLConnection) url .openConnection(); urlConn
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秒)。嘿,我在手机或平板电脑上运行以下代码。我提供给你我的代码看看 代码:
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);
}
}
公共类URLConnectionCheck扩展活动{
text查看结果text;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u urlconnection\u check);
resultText=(TextView)findViewById(R.id.resultTextView);
新建AsynchTaskTest().execute();
}
私有类AsynchTaskTest扩展了AsyncTask
{
@凌驾
受保护字符串doInBackground(无效…参数){
字符串路径=”http://echo.jsontest.com/key/value/one/two";
HttpURLConnection-urlConnection=null;
BufferedReader reader=null;
试一试{
URL=新URL(路径);
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.connect();
InputStream InputStream=urlConnection.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(inputStream));
StringBuffer=新的StringBuffer();
字符串行=”;
而((line=reader.readLine())!=null){
buffer.append(第+行“\n”);
}
返回buffer.toString();
}
捕获(例外e)
{
e、 printStackTrace();
返回“异常”;
}
}
@凌驾
受保护的void onPostExecute(字符串避免){
super.onPostExecute(避免);
resultText.setText(避免);
}
}
}
结果在我的屏幕上显示为:
Ankit,这与请求无关,因为请求可能有效,也可能无效,虽然我看到响应存在,但在某些情况下,我没有收到程序的响应。显然,这是与平板电脑设置或前台处理相关的某种问题。目前,一切都很好,尽管我没有改变任何东西,但在另一个时间它停止了工作。我在前台模式下有一个很长的过程。谢谢你努力寻找解决方案,我给你加了一个加号!