我的android应用程序无法与我的PHP文件建立连接

时间:2014-07-17 05:03:48

标签: java php android httpresponse

我在我的WAMP服务器中创建了一个只播放音频的php文件,现在我正在尝试将我的Android应用程序与我的php文件连接,但显然我的response = httpclient.execute(request)为空。 有什么办法可以得到答复吗?我正在创建一个音频流应用程序顺便说一句。 和平。

MainActivity.java

MediaPlayer playMusic;

//To connect PHP files 
HttpClient httpclient;
HttpGet request;
HttpResponse response;
String url;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    final View controlsView = findViewById(R.id.fullscreen_content_controls);
    final View contentView = findViewById(R.id.fullscreen_content);

    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider
            .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
                // Cached values.
                int mControlsHeight;
                int mShortAnimTime;

                @Override
                @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
                public void onVisibilityChange(boolean visible) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                        // If the ViewPropertyAnimator API is available
                        // (Honeycomb MR2 and later), use it to animate the
                        // in-layout UI controls at the bottom of the
                        // screen.
                        if (mControlsHeight == 0) {
                            mControlsHeight = controlsView.getHeight();
                        }
                        if (mShortAnimTime == 0) {
                            mShortAnimTime = getResources().getInteger(
                                    android.R.integer.config_shortAnimTime);
                        }
                        controlsView.animate()
                                .translationY(visible ? 0 : mControlsHeight)
                                .setDuration(mShortAnimTime);
                    } else {
                        // If the ViewPropertyAnimator APIs aren't
                        // available, simply show or hide the in-layout UI
                        // controls.
                        controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
                    }

                    if (visible && AUTO_HIDE) {
                        // Schedule a hide().
                        delayedHide(AUTO_HIDE_DELAY_MILLIS);
                    }
                }
            });

    // Set up the user interaction to manually show or hide the system UI.
    contentView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TOGGLE_ON_CLICK) {
                mSystemUiHider.toggle();
            } else {
                mSystemUiHider.show();
            }
        }
    });

    // Upon interacting with UI controls, delay any scheduled hide()
    // operations to prevent the jarring behavior of controls going away
    // while interacting with the UI.
    findViewById(R.id.fullscreen_content).setOnTouchListener(mDelayHideTouchListener);

   // URL of PHP Script
    url = "http://192.168.0.134/audio/";

    httpclient = new DefaultHttpClient();
    request = new HttpGet(url);
          // Try to connect using Apache HttpClient Library
            try {
                response = httpclient.execute(request);             //HERE
                Log.d("Response of GET request", response.toString());
                // Check if server response is valid
                  StatusLine status = response.getStatusLine();
                  if (status.getStatusCode() != 200) {
                      throw new IOException("Invalid response from server: " + status.toString());
                  }

            }

            catch (Exception e) {
                // Code to handle exception
                e.printStackTrace();
            }

            // response code
            try {
                BufferedReader rd = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent()));

            } catch (Exception e) {
                // Code to handle exception
                Log.d("yes", url);
            }


}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(100);
}


/**
 * Touch listener to use for in-layout UI controls to delay hiding the
 * system UI. This is to prevent the jarring behavior of controls going away
 * while interacting with activity UI.
 */
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (AUTO_HIDE) {
            delayedHide(AUTO_HIDE_DELAY_MILLIS);
        }
        return false;
    }
};

Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
        mSystemUiHider.hide();
    }
};

/**
 * Schedules a call to hide() in [delay] milliseconds, canceling any
 * previously scheduled calls.
 */
private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
}



@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();


    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ip = wifiInfo.getIpAddress();



        @SuppressWarnings("deprecation")
        String ipadd =Formatter.formatIpAddress(ip);

        try {
            String ipAddre;
            String ipAddress = "";

            for(int i= 150; i < 152; i++)
            {
                for(int counter= 1; counter <= 254; counter++)
                {
                    ipAddre = "192.168." + i + "." + counter;
                    ipAddress = ipAddre.toString();
                    Log.d("x",ipAddre);

                    if(ipAddress.equals(ipadd))
                    {
                        try
                        {
                            playMusic = new MediaPlayer();
                            if(i == 150)
                            {
                                Log.d("m",ipAddre);
                                ipAddress ="192.168.150.0";
                                Log.d("y",ipAddre);
                            }
                            else if (i == 151)
                            {
                                Log.d("s",ipAddre);
                                ipAddress ="192.168.151.0";
                                Log.d("p",ipAddre);
                            }

                            if (ipAddress.equals("192.168.150.0"))
                            {
                                playMusic.setDataSource("http://192.168.0.134/audio/bbb1.php");
                            }
                            else if (ipAddress.equals("192.168.151.0"))
                            {
                                playMusic.setDataSource("http://192.168.0.134/audio/bbb2.php");
                            }
                        }
                        catch(Exception e){}
                    }
                }
            }
            playMusic.setOnPreparedListener((OnPreparedListener) this);
            playMusic.prepareAsync();
            playMusic.start();

            Log.d("zxc", ipAddress);
        }
        catch(Exception e){}

        Log.d("qwe", ipadd);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

}

myfile.php     

echo "<embed src=\"testing.mp3.mp3\" autostart=\"true\" loop=\"true\" hidden=\"true\">             </embed>\n"
."<noembed><bgsound src=\"testing.mp3.mp3\" loop=\"infinite\"></noembed>";
?>11

0 个答案:

没有答案