/ Published in: Java
This program for sending data
Expand |
Embed | Plain Text
//MainActivity //============= package com.v3; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.CoreProtocolPNames; import org.json.JSONObject; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TextView Tname,TCountry,TDob,TCity; EditText Ename,ECountry,EDob,ECity; Button btnCreate; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Tname = (TextView) findViewById(R.id.txtName); Ename = (EditText) findViewById(R.id.editName); TCity = (TextView) findViewById(R.id.txtCity); ECity = (EditText) findViewById(R.id.editCity); TCountry = (TextView) findViewById(R.id.txtCountry); ECountry = (EditText) findViewById(R.id.editCountry); TDob = (TextView) findViewById(R.id.txtDob); EDob = (EditText) findViewById(R.id.editDob); { { examineJSONFile(); } }); } void examineJSONFile() { try { JSONObject object=new JSONObject(); object.put("Name", Ename.getText()); object.put("City", ECity.getText()); object.put("Country", ECountry.getText()); object.put("Dob", EDob.getText()); executeHttpPost(str); Log.i("JsonString :", str); Toast.makeText(this, "Json Objects are : " + str,Toast.LENGTH_LONG).show(); } { } } { //This method for HttpConnection try { HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost("http://192.168.1.118:80/androidjsontest/recieve.php"); List<NameValuePair> value=new ArrayList<NameValuePair>(); value.add(new BasicNameValuePair("Name",string)); UrlEncodedFormEntity entity=new UrlEncodedFormEntity(value); request.setEntity(entity); client.execute(request); } } } } //main.xml //============== <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1"> <TableRow > <TextView android:text="Name" android:id="@+id/txtName" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:text="" android:id="@+id/editName" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </TableRow> <TableRow> <TextView android:text="City" android:id="@+id/txtCity" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:text="" android:id="@+id/editCity" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </TableRow> <TableRow > <TextView android:text="Country :" android:id="@+id/txtCountry" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:text="" android:id="@+id/editCountry" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </TableRow> <TableRow > <TextView android:text="DOB" android:id="@+id/txtDob" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:text="" android:id="@+id/editDob" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </TableRow> <TableRow > <Button android:text="Generate" android:id="@+id/btnGen" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> </TableLayout> </LinearLayout> //menifest.xml set the INTERNET ACCESS premission
Comments
Subscribe to comments
You need to login to post a comment.

Hi, I'm starting with Android developing. I need to make a login form app and I tried to use this app for guideance, but I can't reach the way to send the info from the "EditTexts" containers to the webpage I have as target.
I need to send the info "Usuari" and "Password" to this URL: http://stats.serhstourism.com/?U=USUARI&K=PASSWORD
How can I make it possible to change the toast message for the response of the user and password data to the POST method via Json?
Can you lend me a hand please?
Thanks a lot!
Franco