Posted By

variable3 on 05/09/11


Tagged

json android


Versions (?)

android data sending through post method


 / Published in: Java
 

This program for sending data

  1. //MainActivity
  2. //=============
  3. package com.v3;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.NameValuePair;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. import org.apache.http.message.BasicNameValuePair;
  16. import org.apache.http.params.CoreProtocolPNames;
  17. import org.json.JSONObject;
  18.  
  19. import android.app.Activity;
  20. import android.content.Intent;
  21. import android.os.Bundle;
  22. import android.util.Log;
  23. import android.view.View;
  24. import android.widget.Button;
  25. import android.widget.EditText;
  26. import android.widget.TextView;
  27. import android.widget.Toast;
  28.  
  29. public class MainActivity extends Activity {
  30. TextView Tname,TCountry,TDob,TCity;
  31. EditText Ename,ECountry,EDob,ECity;
  32. Button btnCreate;
  33. String page="";
  34. /** Called when the activity is first created. */
  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38.  
  39. setContentView(R.layout.main);
  40. btnCreate = (Button) findViewById(R.id.btnGen);
  41. Tname = (TextView) findViewById(R.id.txtName);
  42. Ename = (EditText) findViewById(R.id.editName);
  43. TCity = (TextView) findViewById(R.id.txtCity);
  44. ECity = (EditText) findViewById(R.id.editCity);
  45. TCountry = (TextView) findViewById(R.id.txtCountry);
  46. ECountry = (EditText) findViewById(R.id.editCountry);
  47. TDob = (TextView) findViewById(R.id.txtDob);
  48. EDob = (EditText) findViewById(R.id.editDob);
  49.  
  50. btnCreate.setOnClickListener(new Button.OnClickListener()
  51. {
  52. public void onClick(View v)
  53. {
  54. examineJSONFile();
  55. }
  56. });
  57. }
  58.  
  59. void examineJSONFile()
  60. {
  61. try
  62. {
  63. JSONObject object=new JSONObject();
  64. object.put("Name", Ename.getText());
  65. object.put("City", ECity.getText());
  66. object.put("Country", ECountry.getText());
  67. object.put("Dob", EDob.getText());
  68. String str=object.toString();
  69. executeHttpPost(str);
  70. Log.i("JsonString :", str);
  71. Toast.makeText(this, "Json Objects are : " + str,Toast.LENGTH_LONG).show();
  72.  
  73.  
  74. }
  75. catch (Exception je)
  76. {
  77.  
  78. }
  79. }
  80.  
  81. public void executeHttpPost(String string) throws Exception
  82. {
  83. //This method for HttpConnection
  84. try
  85. {
  86. HttpClient client = new DefaultHttpClient();
  87.  
  88. HttpPost request = new HttpPost("http://192.168.1.118:80/androidjsontest/recieve.php");
  89.  
  90. List<NameValuePair> value=new ArrayList<NameValuePair>();
  91.  
  92. value.add(new BasicNameValuePair("Name",string));
  93.  
  94. UrlEncodedFormEntity entity=new UrlEncodedFormEntity(value);
  95.  
  96. request.setEntity(entity);
  97.  
  98. client.execute(request);
  99.  
  100. System.out.println("after sending :"+request.toString());
  101.  
  102. }
  103. catch(Exception e) {System.out.println("Exp="+e);
  104. }
  105.  
  106. }
  107.  
  108. }
  109. //main.xml
  110. //==============
  111. <?xml version="1.0" encoding="utf-8"?>
  112. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  113. android:orientation="horizontal"
  114. android:layout_width="fill_parent"
  115. android:layout_height="fill_parent"
  116. >
  117. <TableLayout android:id="@+id/tableLayout1"
  118. android:layout_width="match_parent"
  119. android:layout_height="wrap_content"
  120. android:stretchColumns="1">
  121. <TableRow >
  122. <TextView
  123. android:text="Name"
  124. android:id="@+id/txtName"
  125. android:layout_width="wrap_content"
  126. android:layout_height="wrap_content"/>
  127. <EditText android:text="" android:id="@+id/editName"
  128. android:layout_width="fill_parent"
  129. android:layout_height="wrap_content"/>
  130. </TableRow>
  131. <TableRow>
  132. <TextView
  133. android:text="City"
  134. android:id="@+id/txtCity"
  135. android:layout_width="wrap_content"
  136. android:layout_height="wrap_content"/>
  137. <EditText android:text="" android:id="@+id/editCity"
  138. android:layout_width="fill_parent"
  139. android:layout_height="wrap_content"/>
  140. </TableRow>
  141. <TableRow >
  142. <TextView
  143. android:text="Country :"
  144. android:id="@+id/txtCountry"
  145. android:layout_width="wrap_content"
  146. android:layout_height="wrap_content"/>
  147. <EditText android:text="" android:id="@+id/editCountry"
  148. android:layout_width="fill_parent"
  149. android:layout_height="wrap_content"/>
  150. </TableRow>
  151. <TableRow >
  152. <TextView
  153. android:text="DOB"
  154. android:id="@+id/txtDob"
  155. android:layout_width="wrap_content"
  156. android:layout_height="wrap_content"/>
  157. <EditText android:text="" android:id="@+id/editDob"
  158. android:layout_width="fill_parent"
  159. android:layout_height="wrap_content"/>
  160.  
  161. </TableRow>
  162. <TableRow >
  163. <Button android:text="Generate"
  164. android:id="@+id/btnGen"
  165. android:layout_width="wrap_content"
  166. android:layout_height="wrap_content"/>
  167.  
  168. </TableRow>
  169. </TableLayout>
  170.  
  171. </LinearLayout>
  172. //menifest.xml
  173. set the INTERNET ACCESS premission

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: fransalo88 on November 16, 2011

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

You need to login to post a comment.