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 java.io.IOException;
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.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import java.io.*;
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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.updatedb);
tv = (TextView)findViewById(R.id.TextView02);
Intent i = getIntent();
name = i.getStringExtra("myname");
pnumber = Uri.parse(i.getStringExtra("phonenumber"));
makeJsonObject();
}//onCreate
void makeJsonObject()
{
JSONObject j = new JSONObject();
try
{
j.put("Name", name);
j.put("PNumber", pnumber);
String url = "http://10.0.2.2/mcashserver/update.php";
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)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
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 = itKeys.next();
v = kvPairs.get(k);
nameValuePairs.add(new BasicNameValuePair(k, v));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
HttpResponse response;
response = httpclient.execute(httppost);
return response;
}//doPost
}//UpdateDb
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 :
<?php
$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....
Thanks....