2017-09-05 115 views
0

我想连接到wampserver并通过按按钮(btnget)在android应用程序中为网页内容吐司,但是当我点击按钮时,它不会吐司。问题出在哪里? 我在本地主机页是 “ss.php”无法从本地主机读取

这是连接类:

public class Connect extends AsyncTask { 

public String link=""; 

public Connect(String link){ 

    this.link=link; 
    } 
    @Override 
    protected Object doInBackground(Object[] params) { 
    try{ 
     URL url=new URL(link); 
     URLConnection connection=url.openConnection(); 
     BufferedReader reader=new BufferedReader(new 
    InputStreamReader(connection.getInputStream())); 
     StringBuilder builder=new StringBuilder(); 
     String line=null; 
     while((line=reader.readLine())!=null){ 
      builder.append(line); 
     } 
     MainActivity.data=builder.toString(); 
    }catch (Exception e){ 
    } 
    return ""; 

}} 

这是主要活动类:

public class MainActivity extends AppCompatActivity { 

public static String data=""; 
Button getData; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    /*address of the webpage on localhost:http://IP/path */ 

    new Connect("http://192.168.56.1/localhost/shop/ss.php").execute(); 

    getData=(Button)findViewById(R.id.btnGet); 
    getData.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      Toast.makeText(MainActivity.this,data, Toast.LENGTH_SHORT).show(); 
     }}); 
}; 

}

+0

这取决于你如何配置Apache服务器。但是这个'http:// 192.168.56.1/localhost/shop/ss.php'应该是'http:// 192.168.56.1/shop/ss.php' – RiggsFolly

+0

你确定'192.168.56.1'是ip运行Apache的PC的地址而不是路由器的IP地址? – RiggsFolly

+0

我该如何找到运行Apache的PC的IP地址? – Ayoub

回答

0

如果我m不会误认为WAMP实例本地托管的站点链接应如下所示:http://LocalHost:8080/shop/ss.php(或您在wamp设置中配置的另一个端口号)

+0

但是,如果你在电话代码中使用'localhost',它会认为你正在寻找一个网站'在电话上' – RiggsFolly

+0

是真的。我错过了(你用WAMP在机器外部运行它)。可能问题在于你没有指定港口。 –

+0

OP没有建议他们改变了端口,如果他们使用的是默认端口80,那么不需要 – RiggsFolly