I am using an library called android volley to establish http connection . I am demonstrating it by using my localhost server.
First you have to create a new android project.then you have to import volley library to the project.
for that just follow Video
https://www.youtube.com/watch?v=7x7h90KAQ5E
Now here is the java code
package com.example.hashimmusthafa.applab; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class Web extends AppCompatActivity { Button web; EditText t1,t2; RequestQueue requestQueue; Intent intent; String insertUrl="http://192.168.137.1:80/php/index.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web); getSupportActionBar().hide(); web=(Button)findViewById(R.id.button2); t1=(EditText)findViewById(R.id.editText2); t2=(EditText)findViewById(R.id.editText3); requestQueue= Volley.newRequestQueue(getApplicationContext()); web.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> parameters = new HashMap<String, String>(); parameters.put("t1", t1.getText().toString()); parameters.put("t2", t2.getText().toString()); return parameters; } }; requestQueue.add(request); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_web, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
it is the xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.hashimmusthafa.applab.Web" android:background="#2b2f38">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WEBSITE TO APP" android:id="@+id/textView4" android:textColor="#fff" android:textStyle="bold" android:textSize="18dp" android:textIsSelectable="true" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" />
<EditText android:layout_width="fill_parent" android:layout_height="50dp" android:id="@+id/editText2" android:background="#fff" android:layout_centerVertical="true" android:layout_alignParentStart="true" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter App Name" android:id="@+id/textView5" android:textColor="#fff" android:textSize="15dp" android:layout_above="@+id/editText2" android:layout_alignParentStart="true" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SUBMIT" android:id="@+id/button2" android:layout_marginTop="66dp" android:background="#fff" android:layout_below="@+id/editText2" android:layout_alignStart="@+id/textView4" android:layout_alignEnd="@+id/textView4" />
<EditText android:layout_width="fill_parent" android:layout_height="50dp" android:id="@+id/editText3" android:background="#ffffff" android:layout_alignParentStart="true" android:layout_below="@+id/textView8" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Web URL" android:id="@+id/textView8" android:layout_below="@+id/textView4" android:layout_alignParentStart="true" android:layout_marginTop="58dp" android:textSize="15dp" android:textColor="#ffffff" />
</RelativeLayout>
now we have to create the webapp.
here is the code.create a file index.php and put the codes inside it.
<?php
$t1=$_REQUEST['t1'];
$t2=$_REQUEST['t2'];
$myfile = fopen("appname.doc", "a") or die("Unable to open file!");
fwrite($myfile,$t1."\n");
fclose($myfile);
$myfiles = fopen("website.doc", "a") or die("Unable to open file!");
fwrite($myfiles,$t2."\n");
fclose($myfiles);
?>
it will create two word docs and will add the typed words to the files
Comments
Post a Comment