【问题标题】:json data send using post in android在android中使用post发送json数据
【发布时间】:2011-08-05 03:48:09
【问题描述】:

我想使用 android 中的 post 方法将我的数据以 json 的形式发送到服务器。

这是我的格式 |data1|data2|data3||data11|data22|data33

希望有一些例子,因为我很难掌握 post 方法的过程。

有什么想法吗?

编辑:

我的json格式|data1|data2|data3||data11|data22|data33|

其中每个数据都是纯文本(文本从数据库中获取)

如何创建它??

【问题讨论】:

    标签: android json gps


    【解决方案1】:

    这篇博文似乎就是在谈论这一点。

    Post JSON Using Android And HttpClient

    编辑:我看到了你的回复。就是这样。希望这能解决问题:)

    public static void main(String[] args) {
        File file = new File("<path to json file>");
        FileInputStream fis;
        String json = "";
    
        try {
            fis = new FileInputStream(file);
    
        BufferedInputStream bis = new BufferedInputStream(fis);
        DataInputStream  dis = new DataInputStream(bis);
    
          // dis.available() returns 0 if the file does not have more lines.
          while (dis.available() != 0) {
             json += dis.readLine();
          }
    
          // dispose all the resources after using them.
          fis.close();
          bis.close();
          dis.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    

    基本上在此之后,您将创建要发送的字符串实体

    StringEntity st = new StringEntity(json.toString());
    

    然后只需按照该链接中的步骤进行操作

    哈哈,编辑您的第二个问题:只需使用数据库中的文本创建一个字符串。这里的所有都是它的。然后像上面那样创建一个 StringEntity。

    【讨论】:

    • thx 链接,我看了一眼,我在创建 json 时遇到了问题。?我怎样才能创造它?我需要撅嘴 { } 吗?
    • 感谢 manish burman 为您编辑的答案,抱歉,现在我只是编辑我的问题,希望您能看一眼 thx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 2013-11-18
    相关资源
    最近更新 更多