Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Client Android JSON--- Server side PhP .....error..!!

Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hiee, I am new to Android and PhP. I wanted to learn and after trying out a few codes i tried out sending data from android emulator to the server. But I am getting errors and couldnt find any solution that could help. Hoping all the coding masters here will help me out.

I am initially sending data in JSON , setting it in HttpClient requests and then trying to get that data on the server side.....but it is not getting the data at all and shows an error....

My client side code is :

package com.db;

import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
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.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class UpdateDb extends Activity
String name;
Uri pnumber;
TextView tv;

protected void onCreate(Bundle savedInstanceState)

tv = (TextView)findViewById(;

Intent i = getIntent();
name = i.getStringExtra("myname");
pnumber = Uri.parse(i.getStringExtra("phonenumber"));


void makeJsonObject()

JSONObject j = new JSONObject();

j.put("Name", name);
j.put("PNumber", pnumber);

String url = "";

Map<String, String> kvPairs = new HashMap<String, String>();

kvPairs.put("data", j.toString());

HttpResponse re = doPost(url, kvPairs);

int statusCode = re.getStatusLine().getStatusCode();
catch (JSONException e)
catch (ClientProtocolException e)
catch (IOException e)


public HttpResponse doPost(String url, Map<String, String> kvPairs) throws ClientProtocolException, IOException
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

if (kvPairs != null && kvPairs.isEmpty() == false)
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(kvPairs.size());
String k, v;
Iterator<String> itKeys = kvPairs.keySet().iterator();

while (itKeys.hasNext())
k =;
v = kvPairs.get(k);
nameValuePairs.add(new BasicNameValuePair(k, v));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response;
response = httpclient.execute(httppost);
return response;



For this code...i tried checking the status code for Http and it showed 200 .....
Ths JSON object is also perfectly constructed.........
I am using Local server initially as i want to just learn the basic first.....

On the server side.. I want to receive this data and display the received data and also decode jason so that i can store it in my database....
I've only tried the first step of 1st receiving and displaying the json....

Here is the server side code :


$data = $_POST['data'];
echo $data;

This is giving me the following error :

Notice: Undefined index: data in C:\wamp\www\mcashserver\update.php on line 2

I tried many things for past one week but couldnt find a solution. Kindly suggest me a correct code required.
I've already checked the other threads on this forum to learn a lot....

WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic