【问题标题】:how to send and receive file in JSON between android and node js如何在android和node js之间以JSON格式发送和接收文件
【发布时间】:2016-03-24 21:51:58
【问题描述】:

我想使用套接字将 JSON 格式的文件发送到 express 节点 js 服务器,我不知道该怎么做,我从 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;
    }


}

【问题讨论】:

  • 你的问题太宽泛了..

标签: android json node.js sockets express


【解决方案1】:

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

new Gson().toJson(DTO);

What is Data Transfer Object DTO

Gson user guide

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 2016-11-23
    • 2013-08-05
    相关资源
    最近更新 更多