我想連接到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();
}});
};
}
這取決於你如何配置Apache服務器。但是這個'http:// 192.168.56.1/localhost/shop/ss.php'應該是'http:// 192.168.56.1/shop/ss.php' – RiggsFolly
你確定'192.168.56.1'是ip運行Apache的PC的地址而不是路由器的IP地址? – RiggsFolly
我該如何找到運行Apache的PC的IP地址? – Ayoub