duanliao6789 2015-09-03 06:42
浏览 114

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

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);
  • 写回答

1条回答 默认 最新

  • doubaolan2842 2015-09-03 06:59
    关注

    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. It will be super easy to do it this way. Also, you can set headers in its setHeader method.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题