2017-07-27 10 views
0

AndroidStudioで(私のアプリを通して)ウェブサイトのhtmlソースを取得しようとしている場合、ページには終了メッセージと異なるコンテンツが表示され、私はウェブビューも使用していません。Android php connection

私は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を有効にする?

+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

のようなJavaScriptコードを扱うことができないHTTPリクエストを行う必要があります。あなたが提供したURLはおそらく、いくつかのjavascriptを実行して、別のページにwindow.locationのリダイレクトを送信します。 HTTPリクエストではブラウザのようにjavscriptを実行してHTMLをそのまま読み込むので、これを処理できません。

これを修正するには、リダイレクトコードをPHP自体に移動する必要があります。これを行うには、PHPのheader()関数を使用します。

+0

ありがとう –

0

ありがとう、私はそれを感謝します。しかし、私は本当に私は000webhostを使用しているホストを変更した後、私のwebhostingからエラーを発見しました。

最初のウェブホスティングは、javascriptを有効にするボットを実行していました。

私はすべてのあなたの苦労を感謝します。^_^