2016-04-09 8 views
2

からレスポンスを受信して​​いない、私のPHPサーバでは私のAndroidのクライアントに非常に単純な文字列をエコーされます。AndroidはデバッグのためにPHPサーバー

<?php echo "I REALLY LIKE PIE" ?> 

は私のAndroidのクライアントは、このような文字列を受信するために、次のコードを持っています

URL url = new URL("http://192.168.0.107/index.php"); 
     urlConnection = (HttpURLConnection) url.openConnection(); 


     urlConnection.setReadTimeout(10000); 
     urlConnection.setConnectTimeout(15000); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     urlConnection.connect(); 

     int HTTPResult = urlConnection.getResponseCode(); 
     if (HTTPResult == HttpURLConnection.HTTP_OK) { 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
      String output = ""; 
      while ((output = bufferedReader.readLine()) != null) { 
       returnedString.append(output); 
      } 
      bufferedReader.close(); 

     } 

     urlConnection.disconnect(); 

しかし、変数 "output"はnullであるため、返される文字列もnullです。なぜ私は応答がないのですか?

+0

http://192.168.0.107/index.phpローカルIPおよびおそらくあなたのAndroidデバイスとのアクセスすることはできません。 –

+0

はHTTPResult? –

+0

192.168.0.107の値が、それは、ネットワークのいただきました?接続成功ですIPアドレスまたはあなたのプロジェクトがセットアップされているシステムのIPアドレス? あなたのシステムのIPアドレスなら、それは直接動作しません。 ネットワークIPアドレスでプロジェクトがサーバーに設定されている場合は、デバイスをネットワーク(Wi-Fi経由)に接続できます。あなたが共有しているプロジェクトリソースにアクセスすることができます。 –

答えて

2
  1. デバッグ目的で指定したログを追加するか、ブレークポイント付きのアンドロイドデバッガを使用します。
  2. php configファイルに必要な変更を加えて、携帯電話からアクセスできるようにしてください。 これはうまくいくはずです。問題が解決しない場合は、教えてください。 (私はその後JSONオブジェクトを解析するためのいくつかのマイナーな変更でこのコードを使用しているが、あなたの場合はその単なる文字列以来、それが動作するはずです。
  3. ずっと、より良い容易にするために

    と最適化、使用OkHttpやバレーボール

    URL url = new URL("http://192.168.0.107/index.php"); 
    urlConnection = (HttpURLConnection) url.openConnection(); 
    BufferedReader reader = null; 
    
    urlConnection.setReadTimeout(10000); 
    urlConnection.setConnectTimeout(15000); 
    urlConnection.setRequestMethod("POST"); 
    urlConnection.setDoOutput(true); 
    urlConnection.setDoInput(true); 
    urlConnection.setRequestMethod("GET"); 
    urlConnection.connect(); 
    
    // Read the input stream into a String 
    InputStream inputStream = urlConnection.getInputStream(); 
    StringBuffer buffer = new StringBuffer(); 
    if (inputStream == null) { 
    
    // Nothing to do. 
    
    } 
    reader = new BufferedReader(new InputStreamReader(inputStream)); 
    
    String line; 
    while ((line = reader.readLine()) != null) { 
    // Since it's JSON, adding a newline isn't necessary (it won't affect  parsing) 
    // But it does make debugging a *lot* easier if you print out the completed 
    // buffer for debugging. 
    buffer.append(line + "\n"); 
    } 
    
    
    bufferedReader.close(); 
    
    } 
    
    urlConnection.disconnect(); 
    
+0

Apache httpd.confファイルの変更点は何ですか?私は他の図書館にはいないので、23番目の図書館が提供する図書館でこの作業を完了することをお勧めします。 – darkterbears

+0

httpd.confファイルで、「すべてから拒否する」という用語を「すべてから許可」に変更します。上のコードはうまくいきましたか?すべてのログはコンソールに届きましたか? – codeyourstack

関連する問題