2011-11-29 130 views
0

我已经在android中创建了本地主机服务器,我正在尝试与我的ajax调用进行通信。我成功连接到了服务器,但无法接收response.xmlhttp状态返回0 only.if我创建了服务器作为国家环保总局本地主机服务器在android中

Ajax调用

function loadXMLDoc() 
{ 
    var xmlhttp; 

    if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
     } 
    else 
     {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
    xmlhttp.onreadystatechange=function() 
     { 
     document.getElementById("myDiv3").innerHTML=xmlhttp.readyState+""; 
     // document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
     if (xmlhttp.readyState==4) 
     { 
      document.getElementById("myDiv1").innerHTML=xmlhttp.status+"";   
      document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 

     } 
    // document.getElementById("myDiv2").innerHTML="2"; 
     } 
    xmlhttp.open("GET","http://localhost:8888/Server",true); 
    //xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
    xmlhttp.send(); 

} 

服务器代码

try{ 
      String fromclient; 
      String toclient; 

      ServerSocket Server = new ServerSocket (8888); 

      System.out.println ("TCPServer Waiting for client on port 8888"); 

      while(true) 
      { 
      Socket connected = Server.accept(); 
      System.out.println(" THE CLIENT"+" "+ 
      connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED "); 



      PrintWriter outToClient = 
       new PrintWriter(
        connected.getOutputStream(),true); 
      outToClient.write("<html><head>hai</head></html>"); 
      connected.close(); 

      } 
     } 
      catch(Exception e) 
      { 

      } 

     } 

回答

1

术语“localhost”在这里根本不正确。 “开发机器”或“主机”会让我更加准确。

反正变化:

xmlhttp.open("GET","http://localhost:8888/Server",true); 

及用途:

xmlhttp.open("GET","http://10.0.2.2:8888/Server",true); 

记住要检查你的防火墙/ iptables和所有的东西。我没有检查你的代码是否有任何其他错误,但是这应该是关于连接的技巧。

相关问题