简体   繁体   English

如何发送创建一个JSON字符串并将其从服务器发送到android?

[英]How to send create a JSON string and send it from server to android?

In my application a user will preform a HTTPrequest to retrieve data from the server. 在我的应用程序中,用户将HTTPrequest形成HTTPrequest以从服务器检索数据。

At the moment on the server side, i preform a database select statement and then use the ResultSetDynaBean to retrieve each row and convert to an object and store in an ArrayList . 在服务器端,我预先形成一个数据库select语句,然后使用ResultSetDynaBean检索每一行并转换为一个对象并存储在ArrayList this all works fine. 一切正常。

 ArrayList<ParkingSpot> spotsList

I then convert eachobject to a JSON string using google GSON library 然后我使用谷歌GSON库将eachobject转换为JSON字符串

ArrayList<String> jsonStrings = new ArrayList<String>();
    Gson gson = new Gson();
    for (ParkingSpot ps : spotsList) {
        String json = gson.toJson(ps);
        jsonStrings.add(json);
    }

Each json string looks like this 每个json字符串都是这样的

{"address":"York Road","zone":"Green","startTime":7.0,"endTime":24.0,"timeAdded":"Jun 16, 2011 11:53:27 AM","psId":898}

there can be up to 1000 of the above strings that i need to send 我需要发送多达1000个上述字符串

As you can see i add each to a String ArrayList . 如您所见,我将每个添加到String ArrayList i do not think this is correct. 我不认为这是正确的。

How should i go about sending the information to an android phone. 我应该如何将信息发送到Android手机。

From the GSON library i can call the below on the android phone 从GSON库我可以在Android手机上调用以下内容

 Spot spot = gson.fromJson(jsonString, Spot.class);
          System.out.println(spot);

But i do not know how retrieve the jsonString from the response of my Servlet (i also do not know how to set it on the servlet side either) 但我不知道如何从我的Servlet的响应中检索jsonString(我也不知道如何在servlet端设置它)

I don't have too much experience with parsing JSON strings but to retrieve a string from the response you can use the following: 我没有太多解析JSON字符串的经验,但是要从响应中检索字符串,您可以使用以下内容:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.google.com"); //replace URL to your service

httpResponse = client.execute(request);
HttpEntity entity = httpResponse.getEntity();

if(entity != null){
    InputStream is = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    while((line = reader.readLine()) != null){
        sb.append(line + "\n");
    }
    JSONString = sb.toString();
}

You can of course use POST method too. 你当然也可以使用POST方法。

Hope this helps :D 希望这会有所帮助:D

I see that you use gson. 我看到你使用gson。 It is really easy with it: 这真的很容易:

Serverside: 服务器端:

ArrayList<ParkingSpot> spotsList = ...
String json = gson.toJson(spotsList);

Thats all on the server side. 这一切都在服务器端。

Clientside: 客户端:

To make a connection and read the response use the answer of marqss. 要建立连接并阅读响应,请使用marqss的答案。 To get the json into a list use this: 要将json放入列表,请使用以下命令:

List<ParkingSpot> list = gson.fromJson(json, new TypeToken<List<ParkingSpot>>(){}.getType());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM