Android HTTP Post在模拟器上工作,但在我的真实设备上不使用



我正在使用上面的代码从我的Web主机获取信息。这很好奇,但是该脚本在AVD中效果很好,但在我的真实设备上效果不佳(三星Galaxy Note)。它没有任何例外。

try{
    HttpClient htClient = getHttpClient();
    HttpPost htPost = new HttpPost("http://myworkingurl/access.php"); 
    Log.i("MyLog", "Print 1");  // this is outputed to LogCat
    HttpResponse htResponse = htClient.execute(htPost);
    Log.i("MyLog", "Print 2"); // no output here
} catch (ClientProtocolException ce) {
    Log.i("MyLog", ce.getMessage()); // no output here
} catch (IOException e) {
    Log.i("MyLog", e.getMessage()); // no output here
}

logcat中没有错误。它输出"打印1",跳过TRY块中的所有剩余代码,而称此方法的类给出了带有void消息的异常。注意:我将用途带标签android.permision.internet放在用途后,并在模拟器中正常工作。

请帮助!

您最好在异步中尝试相同的代码。可能是由于严格的模式。

class ARequestTask extends AsyncTask<String, String, String>{
        @Override
        protected String doInBackground(String... params) {
         //http request here
        //return the response as string
         }
       @Override
       protected void onPostExecute(String result) {
          // use the result as you wish
       }

最新更新