I'm trying to make a post request in android to php but it is not going here is my code...
package bk.girl;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendRequest(View view)
{
try
{
String data = URLEncoder.encode("name", "UTF-8")
+ "=" + URLEncoder.encode("bk", "UTF-8");
URL url=new URL("http://bharath.myartsonline.com/father.php");
URLConnection con=url.openConnection();
con.setDoOutput(true);
OutputStreamWriter os=new OutputStreamWriter(con.getOutputStream());
os.write(data);
}
catch (Exception e){}
}
}
Here is my xml file..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/styles"
android:orientation="vertical"
tools:context="bk.girl.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendRequest"
/>
</LinearLayout>
</LinearLayout>
I have added Internet permission in manifest file too even then the request is not going...
here is my php script...
<?php
extract($_POST);
file_put_contents("file.html", $name);
?>
The file in server is not creating ...Plz help..