简体   繁体   English

将数据从Android服务发送到服务器

[英]Send data from Android Service to Server

I am trying to to send the database record from android to server using JSON. 我正在尝试使用JSON将数据库记录从android发送到服务器。 android code works fine(no error is displayed). android代码工作正常(不显示错误)。 But no any value is displayed in server page. 但是在服务器页面上没有显示任何值。 Below is the android and php code. 以下是android和php代码。

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);

I suggest that instead of using HttpClient and all you use google's own volley library and return your hashmap list in its getParams() overriden method. 我建议不要使用HttpClient而是使用Google自己的凌空库,并在其getParams()重写方法中返回哈希表列表。 It will be super easy to do it this way. 用这种方法做起来将非常容易。 Also, you can set headers in its setHeader method. 另外,您可以在其setHeader方法中设置标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM