json数据在android中使用post发送

时间:2022-10-22 22:48:36

i want to send my data in form of json to server using post method in android.

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

here is my format |data1|data2|data3|<>|data11|data22|data33

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

hope some example since i difficult to catch the procedure of post method.

希望有一些例子,因为我很难抓住post方法的程序。

Anyidea?

Edit:

my json format |data1|data2|data3|<>|data11|data22|data33|

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

where each data is a plain text (text get from database)

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

how can create it??

怎么能创造它?

1 个解决方案

#1


1  

This blog post seems to talk about just that.

这篇博文似乎只谈到了这一点。

Post JSON Using Android And HttpClient

使用Android和HttpClient发布JSON

Edit: I saw your reply. Here's how. Hope this does the trick :)

编辑:我看到了你的回复。这是如何做。希望这样做诀窍:)

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

}

Essentially after this you would create the string entity to send

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

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

Then just follow the steps in that link

然后只需按照该链接中的步骤操作即可

Haha, edit for your 2nd question: Just create a string with the text from the database. Thats all there is to it. Then create a StringEntity like the one above.

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

#1


1  

This blog post seems to talk about just that.

这篇博文似乎只谈到了这一点。

Post JSON Using Android And HttpClient

使用Android和HttpClient发布JSON

Edit: I saw your reply. Here's how. Hope this does the trick :)

编辑:我看到了你的回复。这是如何做。希望这样做诀窍:)

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

}

Essentially after this you would create the string entity to send

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

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

Then just follow the steps in that link

然后只需按照该链接中的步骤操作即可

Haha, edit for your 2nd question: Just create a string with the text from the database. Thats all there is to it. Then create a StringEntity like the one above.

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