title: Java - Android Studio - HttpURLConnection Post to PC PHP file
body: <p>My application works on my PC's Java Netbeans, posting variables to a local php page, echoing the post.</p>
  <p>the PHP script looks like this:</p>
  <pre><code>echo 'VAR' . htmlspecialchars($_POST[&quot;post&quot;]) . 'END' ;
  </code></pre>
  <p>With the root :C:\xampp\htdocs\urlct2ws</p>
  <p>I am trying to achieve this same functionality using Android Studio, running the application on my Android Device. (both devices connected to the same network)</p>
  <p>My code below attempts the connection when a button is pressed.</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          //predefined code - Start
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          //Predefined Code - End
  
          //Button Actions - Start
          final Button button = (Button) findViewById(R.id.btnCon);
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
  
                  String display = &quot;&quot;;
  
                  CallAPI caller = new CallAPI();
                  display = caller.execute().toString();
                  makeToast(display);
              }
          });
          //Button Actions - End
  
      }
  
      public void makeToast(String text) {
  
          Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
  
      }
  
  
      public class CallAPI extends AsyncTask &lt;String, String , String &gt;{
  
          public CallAPI(){
          }
  
          @Override protected void onPreExecute(){
              super.onPreExecute();
          }
  
          @Override
          protected String doInBackground(String... strings) {
  
              String response = &quot;&quot;;
  
              try{
                  String urlString = &quot;http://10.0.0.21/urlct2ws/index.php&quot;;
                  String data = &quot;post=name&quot;;
  
                  URL url = new URL (urlString);
                  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  
                  urlConnection.setUseCaches(false);
                  urlConnection.setReadTimeout(5000);
                  urlConnection.setConnectTimeout(5000);
                  urlConnection.setDoOutput(true); //allows writing to the
                  urlConnection.setDoInput(true);
                  urlConnection.setRequestMethod(&quot;POST&quot;);
  
                  urlConnection.connect();
  
  
                  OutputStream os = urlConnection.getOutputStream();
                  BufferedWriter writer = new BufferedWriter(
                          new OutputStreamWriter(os, &quot;UTF-8&quot;));
                  writer.write(data);
                  writer.flush();
                  writer.close();
                  os.close();
                  //Post Code - End
  
                  //Receiving Post Response - Start
                  BufferedReader in = new BufferedReader(new InputStreamReader(
                          urlConnection.getInputStream()));
                  String inputLine;
                  while ((inputLine = in.readLine()) != null) {
                      response = response + inputLine;
                  }
                  in.close();
                  //Receiving Post Response - End
  
  
              }catch (Exception e){
  
  
              }
  
  
              return response;
          }
  
  
      }
  
  }
  </code></pre>
  <p>Also i do have the following in the AndroidManifest.xml file:</p>
  <pre><code>  &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
  </code></pre>
  <p>The code seems to connect to the file, but the response is &quot;com.example.javaurl.MainActivity$CallAPI@ (7 character code) &quot;</p>