简体   繁体   English

Genymotion无法连接到服务器?

[英]Genymotion unable to connect to server?

I have an app that sends a http request to a server and receives a JSON for processing. 我有一个将HTTP请求发送到服务器并接收JSON进行处理的应用程序。 I test it on both a physical device and Genymotion. 我在物理设备和Genymotion上都对其进行了测试。

The app runs fine on the physical device but on Genymotion throws NetworkOnMainThreadException . 该应用程序在物理设备上运行良好,但在Genymotion上抛出NetworkOnMainThreadException

I tracked the exception and this is the part with the issue: 我跟踪了异常,这是问题所在:

..
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));

// Exception on this line:
HttpResponse httpResponse = httpClient.execute(httpPost);
//
HttpEntity httpEntity = httpResponse.getEntity();
...

It seems that the Genymotion can't connect to server to execute the request. 看来Genymotion无法连接到服务器来执行请求。 But its browser loads the sites just fine. 但是它的浏览器可以很好地加载网站。

So, anyone knows what's going wrong here? 所以,有人知道这里出了什么问题吗?

I found what the problem was: 我发现了问题所在:

As explained in this answer , since API 11, the NetworkOnMainThreadException is thrown to inform using long-running tasks (like http communications) in the main thread. 如该答案中所述 ,从API 11开始,将抛出NetworkOnMainThreadException以在主线程中使用长时间运行的任务(例如http通信)进行通知。

By using AsyncTask the problem was resolved and everything worked as it should. 通过使用AsyncTask ,问题得以解决,一切都按AsyncTask进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM