我想从我正在使用 xampp 的服务器读取数据)但数据是空的,这是我的连接活动:
public String link="";
public AsyncTaskConnect(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 static String data="";
TextView txthello;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txthello=(TextView)findViewById(R.id.txthello);
new AsyncTaskConnect("http://192.168.1.2/digikala/test.php").execute();
txthello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,data,Toast.LENGTH_LONG).show();
}
});
}
但它不起作用,我该怎么办?