I am trying to to send the database record from android to server using JSON. android code works fine(no error is displayed). But no any value is displayed in server page. Below is the android and php code.
package com.example.income;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.*;
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.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.IntentService;
import android.content.Intent;
import android.database.Cursor;
import android.util.*;;
public class Background extends IntentService
{
List<HashMap<String, String>> contacts = new ArrayList<HashMap<String, String>>();
HashMap<String, String> contact = new HashMap<String, String>();
public Background()
{
super("This is the simple background class");
}
@Override
protected void onHandleIntent(Intent intent)
{
Log.v("message","This is simple background service");
Db db =new Db(Background.this,"simple",null,3);
Cursor c= db.getData();
if(c.moveToFirst())
{
do
{
String num,dater;
num = c.getString(c.getColumnIndex("phone_number"));
dater =c.getString(c.getColumnIndex("date"));
contact.put("contact", num);
contact.put("date", dater);
contacts.add(contact);
}
while (c.moveToNext());
try {
sendData();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void sendData() throws ClientProtocolException, IOException, JSONException
{
Log.v("Let's C","Will it go here?");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.56.1/android.php");
JSONArray array= new JSONArray(contacts);
StringEntity se = new StringEntity(array.toString());
httppost.setEntity(se);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
HttpResponse response= httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
if(entity!=null)
{
Log.i("response",EntityUtils.toString(entity));
}
}
}
Pho code
$json = $_POST['HTTP_JSON'];
$data = json_decode($json,true);
print_r($data);