如何在android和节点js之间以JSON发送和接收文件

时间:2016-03-24 13:38:15

标签: android json node.js sockets express

我想使用socket将JSON中的文件发送到快速节点js服务器,我不知道该怎么做,我开始使用android代码,但我不知道如何获取它在服务器端。这是android代码:

    package fr.learning_adventure.android.itac.model;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

// Created by Moez on 02/03/16.


public class Learner implements Serializable{

    private final static String JSON_PSEUDO = "pseudo";

    private final static String JSON_MAC = "mac";



    private String pseudo;




    private String mac;

    public Learner(String mac, String pseudo) {
        this.pseudo = pseudo;
        this.mac = mac;

    }


    public Learner(JSONObject object) {
        try {
            this.pseudo = object.getString(Learner.JSON_PSEUDO);
            this.mac = null;

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public String getPseudo() {
        return this.pseudo;
    }

    public void setPseudo(String pseudo) {
        this.pseudo = pseudo;
    }

    public JSONObject toJSON() {
        JSONObject object = new JSONObject();
        try {
            object.putOpt(Learner.JSON_PSEUDO,this.pseudo);
            object.putOpt(Learner.JSON_MAC, this.mac);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
    }


}

1 个答案:

答案 0 :(得分:0)

为什么不创建一个包含要发送的信息的DTO类,然后当你要通过套接字调用发送对象时:

new Gson().toJson(DTO);

What is Data Transfer Object DTO

Gson user guide