2017-07-27 55 views
0

如果我試圖通過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?

+0

這個錯誤到底在哪裏?你可以分享logcat嗎? –

+0

我已添加圖片顯示錯誤@ JoeyPinto –

+0

檢查我的解決方案,我遇到過這個之前 –

回答

-1

你必須做,這樣

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() { 

     } 
    } 
+0

爲什麼投票??? –

+0

對不起,也許是錯誤的,因爲我試過你的解決方案,它非常類似於我的代碼,它工作正常...非常感謝你 –

0

HTTP請求無法處理JavaScript代碼。你提供的URL可能會執行一些JavaScript來發送一個window.location重定向到另一個頁面。 HTTP請求不能處理這個,因爲它不像你的瀏覽器敵人那樣執行javscript,而是按原樣讀取HTML。

要解決此問題,您必須將重定向代碼移至PHP本身。使用PHP header()函數來執行此操作。

+0

非常感謝你,我很感激它 –

0

謝謝大家,我欣賞它,但我發現從我的網站主辦的錯誤,一旦我改變主機,它真的我現在使用000webhost。

第一個虛擬主機運行的是啓用javascript的機器人。

我很欣賞你所有的辛勤工作。^_^