如果我試圖通過AndroidStudio(通過我的應用程序)獲取網站的html源代碼,頁面顯示與結束消息不同的內容,並且我沒有使用網頁視圖或類似的東西。Android的php連接
我有一個hostweb一個PHP文件,我想顯示在一個TextView消息:
<?php
echo "hello from the other side";
?>
和我的Android代碼:
public class MainActivity extends AppCompatActivity {
TextView text;
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
text = (TextView) findViewById(R.id.text);
try
{
String myurl = "http://keeplearning.eb2a.com/hello.php";
new MyAsyncTaskgetNews().execute(myurl);
}catch (Exception ex){}
}
private class MyAsyncTaskgetNews extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String NewsData="";
try
{
URL url = new URL(params[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
NewsData=Stream2String(in);
in.close();
publishProgress(NewsData);
} catch (Exception e) {
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
try {
text.setText(values[0]);
}catch (Exception ex){
ex.printStackTrace();
}
}
}
public String Stream2String(InputStream inputStream) {
BufferedReader bureader=new BufferedReader(new InputStreamReader(inputStream));
String line ;
String Text="";
try{
while((line=bureader.readLine())!=null) {
Text+=line;
}
inputStream.close();
}catch (Exception ex){}
return Text;
}
}
當我運行它我得到這個錯誤
在您的瀏覽器中激活JavaScript。 enter image description here
任何想法我該怎麼辦?或者我啓用JavaScript?
這個錯誤到底在哪裏?你可以分享logcat嗎? –
我已添加圖片顯示錯誤@ JoeyPinto –
檢查我的解決方案,我遇到過這個之前 –