Android应用程序的Http Post中的UnKnownHostException

时间:2011-09-19 13:30:24

标签: android

在Http Post中,值不会发送到服务器。我在下一行获得了 unknownhostexception HttpResponse rs=httpclient.execute(httppost);

http编码:

          HttpClient httpclient=new DefaultHttpClient();

          HttpPost httppost=new HttpPost("http://www.mysite.com/fasttrack/androidapps.php");


          httppost.setEntity(new UrlEncodedFormEntity(data));

          appendLog("7)Value Encoded to url successfully");


      HttpResponse rs=httpclient.execute(httppost); // unknownhostexception got

我在Manifestfile中添加了<uses-permission android:name="android.permission.INTERNET"/> ...

注意:在添加日志文件编码之前,这些编码工作正常......

我的日志文件编码如下:

public void appendLog(String text)
        {      
            //File logFile = new File("alog.txt");
           File logFile = new File("sdcard/alog.file");
           if (!logFile.exists())
           {
              try
              {
                 logFile.createNewFile();
              } 
              catch (IOException e)
              {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
              }
           }
           try
           {
              //BufferedWriter for performance, true to set append to file flag
              BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
              buf.append(text);
              buf.newLine();
              buf.close();
           }
           catch (IOException e)
           {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }
        }   

1 个答案:

答案 0 :(得分:1)

有时Android模拟器浏览器停止工作。因此,尝试从cmd重新启动模拟器,

run->cmd->your_sdk_path->tools->emulator -avd avd_name -dns-server 8.8.8.8