-1

I want to execute the php file which copies the file to different folder from android . I am running the simple code to just execute the path which has the php file but it is giving and not getting executing

Php file when i execute is working but i want to execute from android. This is my java/android code which i have posted below

Compile.java

package com.coded.sandeep;

import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;


public class Compile extends Activity {


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            //Sending the php file path 
            String php_send="http://localhost/Android/App/copy.php?Coords=allahwariya.mp3";

            // want to execute the above path using Http client but it is not working 
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(php_send);
            try
            {   
                HttpResponse resp = client.execute(httpGet);
                System.out.println(resp);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

    }

}

And in my logcat there are no errors also at the same time php code is not being executed

LOGCAT

07-11 01:30:25.930: W/System.err(1359): android.os.NetworkOnMainThreadException
07-11 01:30:25.970: W/System.err(1359):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
07-11 01:30:25.990: W/System.err(1359):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-11 01:30:25.990: W/System.err(1359):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-11 01:30:26.000: W/System.err(1359):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-11 01:30:26.030: W/System.err(1359):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-11 01:30:26.040: W/System.err(1359):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-11 01:30:26.040: W/System.err(1359):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-11 01:30:26.050: W/System.err(1359):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-11 01:30:26.080: W/System.err(1359):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-11 01:30:26.090: W/System.err(1359):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-11 01:30:26.100: W/System.err(1359):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-11 01:30:26.110: W/System.err(1359):     at com.prgguru.example.AsyncTaskExample.onCreate(AsyncTaskExample.java:73)
07-11 01:30:26.130: W/System.err(1359):     at android.app.Activity.performCreate(Activity.java:5231)
07-11 01:30:26.140: W/System.err(1359):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-11 01:30:26.150: W/System.err(1359):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-11 01:30:26.160: W/System.err(1359):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-11 01:30:26.180: W/System.err(1359):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-11 01:30:26.190: W/System.err(1359):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-11 01:30:26.200: W/System.err(1359):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-11 01:30:26.200: W/System.err(1359):     at android.os.Looper.loop(Looper.java:136)
07-11 01:30:26.220: W/System.err(1359):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-11 01:30:26.230: W/System.err(1359):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 01:30:26.230: W/System.err(1359):     at java.lang.reflect.Method.invoke(Method.java:515)
07-11 01:30:26.250: W/System.err(1359):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-11 01:30:26.260: W/System.err(1359):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-11 01:30:26.270: W/System.err(1359):     at dalvik.system.NativeStart.main(Native Method)
07-11 01:30:27.060: D/dalvikvm(1359): GC_FOR_ALLOC freed 246K, 10% free 3032K/3360K, paused 60ms, total 70ms
07-11 01:30:27.240: D/gralloc_goldfish(1359): Emulator without GPU emulation detected.

can anyone pls tell me how to execute the php file from java

Jens
  • 67,715
  • 15
  • 98
  • 113
ANNN CHOOOR
  • 115
  • 2
  • 13
  • how to execute php file it is not executing as well is the method correct which has been implemented above in java program @mihail – ANNN CHOOOR Jul 11 '14 at 05:58

1 Answers1

1

You should use AsyncTask for URL calls and to avoid NetworkOnMainThreadExceptions.

public class Compile extends Activity {


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            new AsyncTaskOperation().execute();

       }// End of oncreate

        private class AsyncTaskOperation extends AsyncTask <String, Void, Void>
        {
            protected void onPreExecute() { /* Activities before URL call*/ }

            protected void onPreExecute() { /* Activities after URL Call*/ }

            @Override
             protected Void doInBackground(String... paramsObj) {

           //Sending the php file path 
            String php_send="http://localhost/Android/App/copy.php?Coords=allahwariya.mp3";

            // want to execute the above path using Http client but it is not working 
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(php_send);
            try
            {   
                HttpResponse resp = client.execute(httpGet);
                System.out.println(resp);
                 if (httpResponse != null){
                     HttpEntity httpEntity = httpResponse.getEntity();
                      try {
               responseString = EntityUtils.toString(httpEntity);
          } catch (ParseException e) {
            } catch (IOException e) {
            }
                  }// End of if resonse is null
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

            return null;
        }


        } // End of class AsyncTaskOperation 


}// End of class compile
ngrashia
  • 9,869
  • 5
  • 43
  • 58
  • how to execute php file it is not executing as well is the method correct which has been implemented above in java program @Nishanthi Grashia – ANNN CHOOOR Jul 11 '14 at 05:57
  • @ANNNCHOOOR: Refer my answer as to how to call and process URL Response. The string 'responseString' will contain the web-service response. – ngrashia Jul 11 '14 at 06:01
  • But i am not able to execute the php file @Nishanthi Grashia – ANNN CHOOOR Jul 11 '14 at 06:23
  • Are you able to do it in the server side? If not, check the server side php code. – ngrashia Jul 11 '14 at 06:24
  • if i use your code i am getting errors near the new AsyncTaskOperation().execute(); ca n u please tell me just how to execute the php file – ANNN CHOOOR Jul 11 '14 at 06:26
  • 1
    What is the error you are getting? – ngrashia Jul 11 '14 at 06:28
  • server side php code is correct when i execute it it is correctly copying the file issue is with how to execute from the java file and also the exception @Nishanthi Grashia – ANNN CHOOOR Jul 11 '14 at 06:28
  • 07-11 02:30:53.510: W/System.err(1501): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused 07-11 02:30:53.560: W/System.err(1501): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183) 07-11 02:30:53.560: W/System.err(1501): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) – ANNN CHOOOR Jul 11 '14 at 06:32
  • i am trying to execute the php file but i am not able to execute at all ....I just want to only execute and copy the file from one folder to another ...php is doing it but java is not executing the php file – ANNN CHOOOR Jul 11 '14 at 06:34
  • 1
    @ANNNCHOOOR: Your URL is local host. that is why it is not working. Please put the proper server URL or ateast the local server IP if using on an emulator. – ngrashia Jul 11 '14 at 06:38
  • oh k sure thank you yaar once again i will try and let you know @Nishanthi Grashia – ANNN CHOOOR Jul 11 '14 at 06:41
  • i will surely do that – ANNN CHOOOR Jul 11 '14 at 07:01