RSS

The Simplest Way to POST Parameters Between Android and PHP (Android SDK)

26 Apr

This article is moved to here.

 
79 Comments

Posted by on April 26, 2011 in Android, web programming

 

Tags: , , , , ,

79 responses to “The Simplest Way to POST Parameters Between Android and PHP (Android SDK)

  1. Mohammad Hussain

    May 6, 2011 at 17:16:26

    Hi
    I have introduced nusoap lib to my php code and consume webservice with Android application. How to make communication with them.

     
  2. terry

    June 5, 2011 at 19:15:15

    do you have screencast in this project??

     
  3. john

    June 27, 2011 at 01:28:48

    what do you mean by on android side? where in eclipse should i place this file?

     
    • fahmirahman

      June 27, 2011 at 10:45:39

      You should create new project on eclipse.. Create the same package name and class name with the scripts above..

       
      • James Mitchee

        January 21, 2012 at 03:01:57

        how to use timertask or alarm for interval refresh of the class so that we can send the data at regular interval even when phone is asleep !

         
      • sachinsurya

        January 25, 2013 at 13:45:33

        I have XAMPP for PHP server
        I m using this code but i m getting error at line no 35-> postData();
        Can you please help me out..

         
  4. sush

    June 27, 2011 at 14:51:30

    how to include php code in eclipse ???

    I am new to android .. plz help..

     
    • fahmirahman

      June 27, 2011 at 16:23:38

      those PHP code on the server side.. not in client (android) side..

       
      • sureshnaidupati

        August 4, 2011 at 20:33:07

        Hi ,

        where i put PHP file in server side

         
  5. sush

    June 28, 2011 at 11:55:48

    hello Mr.fahmirahman,

    can u give me any sample project for client server for android using json…
    It will be helpful for me..

    Thanks in advance..

     
  6. sush

    June 28, 2011 at 12:15:32

    for the above programme it says “application stopped unexpectedly.please try again”..

    what could be the reason…

     
    • fahmirahman

      June 28, 2011 at 13:26:00

      use only the script bellow “Brand new simple code to deliver JSON POST from Android to PHP” or only the script above “Brand new simple code to deliver JSON POST from Android to PHP”..

      you need to learn basic Java programming if you were new to this project, or you can find any tutorial about how-to make android projects using eclipse over the internet..

      this thread only shows how-to build simple client-server application using android as a client and PHP as a server..

       
  7. Debjani

    June 29, 2011 at 11:37:34

    The php side of the script is giving me an error 😦
    Undefined index: HTTP_JSON in C:\wamp\www\post.php on line 2
    Could you help resolve this?

     
    • fahmirahman

      June 30, 2011 at 13:16:55

      you can see at the main.java line 56 – 58 :

      // Post the data:
      httppost.setHeader(“json”,json.toString()); <– this script made the header HTTP_JSON
      httppost.getParams().setParameter(“jsonpost”,postjson);

      So if you dump the $_SERVER on your PHP server side using var_dump($_SERVER) you must be see the HTTP_JSON header there..
      ” Undefined index: HTTP_JSON in C:\wamp\www\post.php on line 2 ” <– this is error code that given by WAMP that notice HTTP_JSON header doesn't exist on your server header.
      There's probably something wrong with your apache config for PHP or I suggest you using LAMPP.. 🙂

       
      • Debjani

        July 2, 2011 at 18:33:44

        Thanks so much for the hint. My error was that I was viewing the php file from my wamp server and expecting some output. I didn’t understand that the URL included the json header. No wonder it was showed me Undefined index error. Thanks so much 🙂

         
  8. sush

    June 30, 2011 at 12:27:54

    Hello Mr. Fahmirahman,

    Thanks for reply…

    I want simple client server programme where tomcat (server) and android (client). with json as string..

    can u give me any example code for this .. I dont know how to write am new to android and tomcat…

    Pls help…

    Thank you.

     
    • fahmirahman

      June 30, 2011 at 13:19:56

      I suggest you better use LAMPP for PHP server rather than TOMCAT.. 🙂

       
  9. sush

    June 30, 2011 at 12:51:12

    i have used the script below “Brand new simple code to deliver JSON POST from Android to PHP”

    but i was not able to see any output in my emulator it just shows the application name…

    pls tell me how to run the programme..

     
    • fahmirahman

      June 30, 2011 at 13:18:53

      did you use APACHE server for your PHP code on your server side?

       
  10. yuvarajan

    July 8, 2011 at 14:57:16

    localhost is not taken in android. it throws exception

     
    • prasad

      June 27, 2012 at 16:04:28

      in android local host mean 10.0.2.2

       
  11. yuvarajan

    July 8, 2011 at 15:30:10

    Undefined variable: HTTP_RAW_POST_DATA in C:\xampp\htdocs\post.php on line 5

    i got this error help me…

     
  12. Prodej chalupy bez realitky

    July 9, 2011 at 01:36:56

    Fine post dude

     
  13. jim

    July 10, 2011 at 23:18:24

    When i var_dump($json and $data) i get both as NULL? can u help

     
  14. guije

    July 30, 2011 at 15:06:10

    hiii friends, how i can connect android json php postgres…best regards

     
  15. swapnil gupta

    August 18, 2011 at 23:52:20

    my client side application is working fine but on server side the data is being received.i am trying to update the database with contents of name and position that is sent through the android json object.the database is not getting updated.do you any suggestions for this?

    my code on server side is:

    name;

    $pos = $data->position;

    echo “Result: \n”;

    echo “————–\n”;

    echo “Name : “.$name.”\n Position : “.$pos;

    mysql_connect ( $dbhost, $dbuser, $dbpass)or die(“Could not connect: “.mysql_error());
    mysql_select_db($dbname) or die(mysql_error());

    $sql=”INSERT INTO android11 VALUES($pos,$name,time())”;

    mysql_query($sql);

    ?>

     
  16. nipa

    August 30, 2011 at 11:56:53

    sir,my php code is following

    and my app code is
    package com.PL;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;

    import org.apache.http.HttpEntity;
    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.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.Activity;
    import android.net.ParseException;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;

    public class PLActivity extends Activity {
    /** Called when the activity is first created. */
    JSONArray jArray;
    String result=null;
    InputStream is = null;
    StringBuilder sb=null;
    int ct_id;
    String ct_name;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ArrayList nameValuePairs = new ArrayList();

    try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(“http://10.0.2.2/City.php”);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    }catch(Exception e){
    Log.e(“log_tag”, “Error in http connection”+e.toString());
    }
    //convert response to string
    try{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,”iso-8859-1”),8);
    sb = new StringBuilder();
    sb.append(reader.readLine() + “\n”);
    String line=”0″;
    while ((line = reader.readLine()) != null) {
    sb.append(line + “\n”);
    }
    is.close();
    result=sb.toString();
    }
    catch(Exception e){
    Log.e(“log_tag”, “Error converting result “+e.toString());
    }
    //paring data

    try{
    jArray = new JSONArray(result);
    JSONObject json_data=null;
    for(int i=0;i<jArray.length();i++){
    json_data = jArray.getJSONObject(i);
    ct_id=json_data.getInt("CITY_ID");
    ct_name=json_data.getString("CITY_NAME");
    }
    }
    catch(JSONException e1){
    Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
    } catch (ParseException e1) {
    e1.printStackTrace();
    }
    }

    }

    but it does not show any output.please help me sir.plzzzzzzzzzzzzzzzzzzzzzzzzz

     
  17. nipa

    August 30, 2011 at 12:00:32

    sir, previous post doesn’t show php code .it is following

    mysql_connect(“localhost”,””,””);
    mysql_select_db(“deal”);
    $sql=mysql_query(“select * from city where CITY_NAME like ‘A%'”);
    while($row=mysql_fetch_assoc($sql))
    $output[]=$row;
    print(json_encode($output));
    mysql_close();

    with php tag

     
  18. 04

    September 9, 2011 at 15:29:09

    name;
    $pos = $data->position;
    echo “Result: \n”;
    echo “————–\n”;
    echo “Name : “.$name.”\n Position : “.$pos;

    $con=mysql_connect(“**********”,”***********”,”*********”);
    if (!$con)
    {
    echo “error”;
    }
    mysql_select_db(“maksans_mmm”,$con);
    $result=mysql_query(“select * from orders”) or die(mysql_error()) ;
    echo “error”;
    while($row=mysql_fetch_array($result))
    {

    echo $row[0];
    echo “”;
    echo $row[1];
    echo “”;
    }
    echo “error”;
    mysql_close($con);

    ?>
    ur code works for me….
    in this above code how to send the $row[0] values to android and how to store it in android?????
    ????

     
  19. Alex Bergsland

    September 17, 2011 at 18:56:31

    Very nice indeed, this will get me started in my GPS oriented Android app =)

     
  20. Manit

    September 22, 2011 at 01:22:16

    Hi,

    Very nice code…

    What if i want use it to get directly json object from JSON server?

     
  21. Sajol

    October 6, 2011 at 12:49:20

    hi,
    i can connect mysql database from android phone.I have created a table with all varchar type fields.I want to search a specific row depending on a string value.I do this as follow.

    String id = “01494”
    ArrayList nameValuePairs = new ArrayList();
    nameValuePairs.add(new BasicNameValuePair(“year”,id));
    ………………………………………………………
    this work and show desired data

    but
    when i use

    String id = “sajol”
    ArrayList nameValuePairs = new ArrayList();
    nameValuePairs.add(new BasicNameValuePair(“year”,id));

    it does not any data.I have data corresponding to id sajol.

    what’s the problem?
    please help me sir as soon as posible.

     
  22. asraf

    October 24, 2011 at 17:20:58

    Hi

    when i start running the JSON example in emulator, i receive the following error in my logcat,

    10-24 10:17:10.971: E/AndroidRuntime(1244): FATAL EXCEPTION: main
    10-24 10:17:10.971: E/AndroidRuntime(1244): java.lang.RuntimeException: Unable to start activity ComponentInfo{a.s.d/a.s.d.JSONPostActivity}: android.os.NetworkOnMainThreadException
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.app.ActivityThread.access$600(ActivityThread.java:122)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.os.Handler.dispatchMessage(Handler.java:99)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.os.Looper.loop(Looper.java:137)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.app.ActivityThread.main(ActivityThread.java:4340)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at java.lang.reflect.Method.invokeNative(Native Method)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at java.lang.reflect.Method.invoke(Method.java:511)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at dalvik.system.NativeStart.main(Native Method)
    10-24 10:17:10.971: E/AndroidRuntime(1244): Caused by: android.os.NetworkOnMainThreadException
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at java.net.InetAddress.getAllByName(InetAddress.java:220)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at a.s.d.JSONPostActivity.postData(JSONPostActivity.java:66)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at a.s.d.JSONPostActivity.onCreate(JSONPostActivity.java:38)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.app.Activity.performCreate(Activity.java:4465)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    10-24 10:17:10.971: E/AndroidRuntime(1244): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
    10-24 10:17:10.971: E/AndroidRuntime(1244): … 11 more

    Can u please help me and explain what i need to do..

     
  23. antony

    November 3, 2011 at 18:45:27

    Nice coding.It’s working for me

     
  24. Nahappogewged

    January 6, 2012 at 06:27:42

    where to buy remeron

     
  25. Swanidhi Singh

    January 7, 2012 at 05:22:18

    Hey ! Thanks for this informative article. I am new to android, and this was just what I needed. Before this, I was trying to use Google API for Spreadsheets but was not very successful.

     
  26. megh

    January 17, 2012 at 15:54:18

    hii fahmirahman,i want your help.I am developing an android simple login page.I have to submit the form from androi and then i want these values to be sent to my php file so that these values can be used in the query i write to access data from database.I am faced with a problem that when i pass values from android emulator these are well shown on my emulator ad not going in php script so as to be used in query.i have used php mail function to check if the values are sent to php script.The mail recieves the fields from android but still not showing on my browser.I am trying to display these values on browser so that i can see if these values can be passed to query.Can you please see the under mentioned code and help me out.
    php code

     
  27. megh

    January 17, 2012 at 15:55:45

    package com.bm.login2;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ByteArrayEntity;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.params.BasicHttpParams;
    import org.apache.http.params.HttpConnectionParams;
    import org.apache.http.params.HttpParams;
    import org.apache.http.protocol.HTTP;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    public class BMlogin2Activity extends Activity {

    TextView result; /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }

    public void postLoginData(ArrayList data) {

    try {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(“path”); //my website url here
    httppost.setEntity(new UrlEncodedFormEntity(data));
    HttpResponse response = httpclient.execute(httppost);
    Log.i(“postData”, response.getStatusLine().toString());
    //Could do something better with response.
    }
    catch(Exception e)
    {
    Log.e(“log_tag”, “Error: “+e.toString());
    }

    }

    private void sendData(String username, String password)
    {
    String fileName = “xyz.php”; ///php file name here where to put data

    //Add data to be send.
    ArrayList nameValuePairs = new ArrayList(2);
    nameValuePairs.add(new BasicNameValuePair(“username”, username));
    nameValuePairs.add(new BasicNameValuePair(“password”,password));
    this.postLoginData(nameValuePairs);
    }

    }

     
  28. megh

    January 17, 2012 at 15:58:27

    my php code is:
    $link = mysql_connect(‘localhost’,’windener’, ‘s4244pnAWw’) or die(mysql_error());
    mysql_set_charset(‘utf8’, $link);
    //selecting database from active connection
    $json = $_SERVER[‘HTTP_JSON’];
    $mail=$_POST[‘username’];
    $pass=$_POST[‘password’];

    //mail(“meghkaur@gmail.com”,$pass,$mail);
    mysql_select_db(‘windener_timemerlin’, $link) or die(mysql_error());
    //return link of the MySQL connection
    $select_userid_query= “select u.empId, u.userID, u.userRoleID, u.status, u.name, u.cid from users u,”
    .” customers c where u.cid=c.cid and c.status=’ACTV’ and u.mail like ‘m_monikar@yahoo.com’ and u.password like (‘e10adc3949ba59abbe56e057f20f883e’) and u.status=’A'”;
    //echo $select_userid_query;
    $result=mysql_query($select_userid_query,$link);
    if($row=mysql_num_rows($result)==1)
    {
    echo”oks “;
    echo $mail;
    }
    else
    {
    echo”fail”;

    }
    mysql_close();

     
  29. Farhan

    February 26, 2012 at 20:36:47

    But how to create this project,Can u give me sample copy of this code.

     
  30. shahzadrocks

    March 23, 2012 at 19:54:50

    @Fahim:Wy arent u replying these questions for a long time.Google redirects here and we dont see any of the query solved which can come in our way?????????Please take care of it…

     
  31. Nancy

    April 12, 2012 at 18:39:59

    What does this code exactly do please

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

    InputStream is = response.getEntity().getContent();

    BufferedInputStream bis = new BufferedInputStream(is);

    ByteArrayBuffer baf = new ByteArrayBuffer(20);

    thanks in advance

     
  32. omayma

    June 16, 2012 at 04:37:36

    please how can send json data from php in wamp server to android in client . can i send it using URL and how i write it.

     
    • Grant

      August 22, 2012 at 23:23:16

      Do you mean WAMPSERVER running on local host ?? Is that the case you have to use a URL something similar to this. http:\\10.0.2.2\phpfilename.php

       
  33. Nashan Haris

    September 26, 2012 at 13:00:41

    I have listview in android.What want is when i click a item the text in the item should go to serach parameter for google or shopping web site. How to pass the parameter.

     
  34. U

    December 28, 2012 at 02:08:12

    Hi Fahmi,

    Thanks for this fantastic post!

    However i have used your code above and getting the following error in the emulator after trying to post the JSON object to my server:

    “An unexpected error occurred”

    Do you have any idea what might be causing that?

    Thanks

     
  35. U

    December 28, 2012 at 02:08:12

    Hi Fahmi,

    Thanks for this fantastic post!

    However i have used your code above and getting the following error in the emulator after trying to post the JSON object to my server:

    “An unexpected error occurred”

    Do you have any idea what might be causing that?

    Thanks

     
  36. gentle

    January 5, 2013 at 04:58:19

    heyy
    thanks for this hope-giving post

    but what should happen if u let the app run?
    ive pasted ur first script without the JSON and well the app starts, i get a blank application and nothing happens

    than i tried to combine ur previous food-database-connection post with this post and it says “application stopped unexpectedly.please try again”

    im using Xampp

    should i use Wampp or Lampp or an other?
    i really dont know what to do anymore ^^

     
  37. Jithinraj

    February 26, 2013 at 22:59:43

    Thanks for this post…. Nice coding…:)

     
  38. ritesh4all2011

    March 12, 2013 at 16:22:12

    plzz hemp me my code is not working:

    package com.example.datafetch;
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.List;

    import org.apache.http.HttpEntity;
    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.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.ListActivity;
    import android.net.ParseException;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;

    public class MainActivity extends ListActivity {

    private List nameValuePairs;
    private JSONArray jArray;

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    String result = null;

    InputStream is = null;

    StringBuilder sb=null;

    String result1=null;

    //http post

    try{

    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(“http://127.0.0.1/chotu/conn.php”);

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpclient.execute(httppost);

    HttpEntity entity = response.getEntity();

    is = entity.getContent();

    }catch(Exception e){

    Log.e(“log_tag”, “Error in http connection”+e.toString());

    }

    //convert response to string

    try{

    BufferedReader reader = new BufferedReader(new InputStreamReader(is,”iso-8859-1”),8);

    sb = new StringBuilder();

    sb.append(reader.readLine() + “\n”);

    String line=””;

    while ((line = reader.readLine()) != null) {

    sb.append(line + “\n”);

    }

    is.close();

    result1=sb.toString();

    }catch(Exception e){

    Log.e(“log_tag”, “Error converting result “+e.toString());

    }

    //paring data

    int fd_id;

    String fd_name;

    try{

    jArray = new JSONArray(result);

    JSONObject json_data=null;

    for(int i=0;i<jArray.length();i++){

    json_data = jArray.getJSONObject(i);

    fd_id=json_data.getInt("FOOD_ID");

    fd_name=json_data.getString("FOOD_NAME");

    }

    }catch(JSONException e){

    Toast.makeText(getBaseContext(), "No Food Found", Toast.LENGTH_LONG).show();

    }catch (ParseException e){

    e.printStackTrace();

    }

    }

    }

     
    • Pedro Martins

      May 7, 2013 at 16:24:10

      change 127.0.0.1 to your ip

       
  39. savan

    March 19, 2013 at 14:25:13

    thanks for this tutorial, it’s very helpfull with me

     
  40. john

    March 27, 2013 at 16:41:12

    can you please provide source code

     
  41. Emre

    May 9, 2013 at 04:43:46

    hi, your post helped to solve my problem. thanks. i shared your post in yhis question.

    http://stackoverflow.com/questions/16447927/how-to-get-json-object-from-android-to-php/16450626#16450626

     
  42. Mohammed Abbas

    May 14, 2013 at 04:11:10

    really i appreciate that ,,, but i have a question how does the my android device “client” connect with the server by the internet or other connection??

     
  43. vin number decoder

    August 4, 2013 at 20:21:19

    Right away I am ready to do my breakfast, later than having my breakfast coming
    again to read additional news.

     
  44. Debra

    September 22, 2013 at 06:14:49

    It helps in development of stamina, agility and speed. There are some online websites that sell hand wrap sellers. People who go for regular training are more cheerful and happy as compared to the people who does not go for it.

     
  45. MarcF

    October 30, 2013 at 01:02:14

    Thanks fahmirahman !
    Finally successfull, althought I had to change something : I had to place a big part of your code into an async task.
    To do such a thing, I’ve been inspired by Chathura here :
    http://codeoncloud.blogspot.fr/2013_07_01_archive.html
    His code works fine, I just adapted it to pass parameters to a php script.
    If anyone here is interested, I’ll post the source code of Activity_main.java.

     
  46. Jalpa Patel

    December 13, 2013 at 13:20:55

    can i send me a full source code of this ?
    my id id : jalpa39@gmail.com

     
  47. Adam

    May 20, 2014 at 04:05:10

    Hey, this is incredibly helpful, but I have a few questions. Could you explain what exactly setHeader() is doing, and what $_SERVER[‘HTTP_JSON’] retrieves from the server?

     
  48. Freda

    September 27, 2014 at 02:35:23

    Greetings! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly?

    My blog looks weird when viewing from my apple iphone.
    I’m trying to find a template or plugin that might be able to resolve this issue.

    If you have any recommendations, please share. Thanks!

     
  49. Suresh Kerai

    November 24, 2014 at 18:44:38

    Thanks…

     
  50. buy runescape million

    December 24, 2014 at 09:55:17

    Oh, It’s a nice post!
    Love you, Thanks!
    Excuse me, Please look at my username! Buying runescape million.

     
  51. Gabiel Matos Rodriguez

    January 23, 2015 at 05:06:10

    My returne from server: Empty
    I/Resultado Servidor:﹕ JSON:
    ————–
    NULL
    Array:
    ————–
    NULL
    Result:
    ————–
    Name :
    Position :

     
  52. Sergio

    February 14, 2015 at 12:39:47

    Hello, Thanksgiving is the only example that really works.

     
  53. Wai Hyn

    March 14, 2015 at 14:20:43

    I love that code

     
  54. imam

    March 30, 2015 at 15:50:52

    nice code…
    can you send all that code to my e-mail plese.
    imamchairul76@gmail.com

     
  55. ashutosh

    July 29, 2015 at 14:53:54

    This is easy and awesome!

     
  56. Michael Gerding

    August 17, 2015 at 18:20:44

    Hello,
    I read your example and want to use it for my project. But it doesn’t work. I don’t an error as response. So i an echo count($dataarray); inide the php document. It returns always size 1.

    If I post the Data as the following

    httppost.setHeader(“json”,postjson.toString());

    , It works. But then I only can send an array of 70 elements because otherwise server returns the following message.

    400 Bad Request

    Bad Request
    Your browser sent a request that this server could not understand.
    Size of a request header field exceeds server limit.

        json
        

    Apache/2.4.12 (Win32) OpenSSL/1.0.1m PHP/5.6.11 Server at localhost Port 80

    Can you help me to find out why post the Data as shown in your example doesn’t work for me?

     
  57. sanal ticaret

    October 26, 2015 at 09:01:22

    Paylaştığınız için teşekkürler

     
  58. Abhinay Srivastava

    March 10, 2016 at 19:46:57

    Sir can u send me code to how to develop a RESTful service in android. As i have to develop a project which have a number of services in a and the user selects the services by clicking on it and the service goes to the server and calculates the amount and shows the total charges.

     
  59. Rubertnam

    September 7, 2018 at 18:07:49

    Работа в интернете

     
  60. Gelianam

    November 1, 2018 at 14:23:52

    Работа в интернете

     
  61. mizoevanatolij

    December 2, 2018 at 14:46:59

    Работа в интернете

     

Leave a comment