【问题标题】:Android php connection安卓php连接
【发布时间】:2017-07-27 19:22:22
【问题描述】:

如果我尝试使用 AndroidStudio(通过我的应用程序)获取网站的 html 源代码,页面会显示不同的内容以及结束消息,而我没有使用网络视图或类似的东西。

我在 hostweb 中有一个 php 文件,我想在 textview 中显示消息:

<?php
echo "hello from the other side";
?>

我的安卓代码是:

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
  • 检查我的解决方案,我以前遇到过这种情况
  • 是的,谢谢先生

标签: javascript php android web-hosting


【解决方案1】:

谢谢大家,我很感激,但是一旦我更改了主机,我发现我的虚拟主机出现了错误,我现在真的在使用 000webhost。

第一个虚拟主机是运行一个启用 javascript 的机器人。

感谢您的辛勤工作。 ^_^

【讨论】:

    【解决方案2】:

    HTTP 请求无法处理 javascript 代码。您提供的 URL 可能会执行一些 javascript 以将 window.location 重定向发送到另一个页面。 HTTP 请求无法处理此问题,因为它不会像您的浏览器敌人那样执行 javscript 并按原样读取 HTML。

    要解决此问题,您必须将重定向代码移至 PHP 本身。使用 PHP header() 函数来执行此操作。

    【讨论】:

      【解决方案3】:

      你必须这样做

      public class MyAsyncTaskgetNews extends AsyncTask<String, String, String> {
      
      
              HttpURLConnection conn;
              URL url = null;
      
      
              @Override
              protected String doInBackground(String... params) {
      
                  try {
             url = new URL(params[0]);
      
                  } catch (MalformedURLException e) {
                      e.printStackTrace();
                      return "exception";
                  }
                  try {
      
                      conn = (HttpURLConnection)url.openConnection();
                      conn.setReadTimeout(15000);
                      conn.setConnectTimeout(1000);
      
                      conn.setDoInput(false);
                      conn.setDoOutput(true);
      
                      Uri.Builder builder = new Uri.Builder();
                      String query = builder.build().getEncodedQuery();
      
                      // Open connection for sending data
                      OutputStream os = conn.getOutputStream();
                      BufferedWriter writer = new BufferedWriter(
                              new OutputStreamWriter(os, "UTF-8"));
                      writer.write(query);
                      writer.flush();
                      writer.close();
                      os.close();
                      conn.connect();
      
                  } catch (IOException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                      return "exception";
                  }
      
                  try {
      
                      int response_code = conn.getResponseCode();
      
                      // Check if successful connection made
                      if (response_code == HttpURLConnection.HTTP_OK) {
      
                          // Read data sent from server
                          InputStream input = conn.getInputStream();
                          BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                          StringBuilder result = new StringBuilder();
                          String line;
      
                          while ((line = reader.readLine()) != null) {
                              result.append(line);
                          }
      
                          // Pass data to onPostExecute method
                          return(result.toString());
      
                      }else{
      
                          return("unsuccessful");
                      }
      
                  } catch (IOException e) {
                      e.printStackTrace();
                      return "exception";
                  } finally {
                      conn.disconnect();
                  }
              }
      
              @Override
              protected void onPostExecute(String result) {
                  mAuthTask = null;
      
                  // result contains the response, do whatever u want to do
              }
      
              @Override
              protected void onCancelled() {
      
              }
          }
      

      【讨论】:

      • 很抱歉,可能是错误的,因为我尝试了您的解决方案,它与我的代码非常相似,并且运行良好......非常感谢您
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2012-05-26
      • 2010-11-02
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      相关资源
      最近更新 更多