【问题标题】:Consume web service providing updating JSON object使用提供更新 JSON 对象的 Web 服务
【发布时间】:2014-11-14 06:28:30
【问题描述】:

我需要一个学校项目的帮助,我需要连接到一个 Web 服务,该服务提供每 3 或 4 秒更新一次的 JSON 文档,使用它并使用其中包含的一些信息。 JSON 如下所示:

{ “名字”:“约翰”,
“姓氏”:“史密斯”,
“isAlive”:是的,
“年龄”:25,
“身高_厘米”:167.6,
“地址”:{
"streetAddress": "21 2nd Street",
“城市”:“纽约”,
“州”:“纽约”,
"邮编": "10021-3100"
},
“电话”:[
{
“类型”:“家”,
"号码": "212 555-1234",
“持续时间”:“32”
},
{
“类型”:“办公室”,
"号码": "646 555-4567",
“持续时间”:“79”
}
]
}

Json 文件每 x 秒更新一次,随机调用添加到文档中, 我需要使用这些信息。

我不知道如何连接到这个本地网络服务,并从这个更新文档中检索这些信息,我想使用 JAVA,但如果有更好的解决方案,请告诉我。

感谢您提供的所有提示。

【问题讨论】:

  • 您提供 JSON 字符串的 Web 服务是否也将 JSON 字符串推送到您的 Web 服务?

标签: java json web-services feed


【解决方案1】:

请参阅此示例,了解如何在 Java 中通过 http 从给定 url 返回 JSONObject。这包括对基本身份验证的支持,您可能需要也可能不需要。

您要做的是使用计时器调用此方法以根据需要刷新您的提要。

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Base64;

...其他进口

public static JSONObject readJSONFeed(String URL, String username,
        String password) throws KeyManagementException,
        UnrecoverableKeyException, NoSuchAlgorithmException,
        KeyStoreException, ClientProtocolException, IOException {

    String auth = username + ":" + password;
    HttpClient httpClient = = new DefaultHttpClient;

    StringBuilder stringBuilder = new StringBuilder();

    // Build HTTP request
    HttpPost httpPost = new HttpPost(URL);
    httpPost.setHeader(
            "Authorization",
            "Basic "
                    + Base64.encodeToString(auth.getBytes(), Base64.NO_WRAP));
    httpPost.setHeader("Accept", "application/json");

    // send the request
    HttpResponse response = httpClient.execute(httpPost);

    // read the result
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }
        inputStream.close();
    } else if (statusCode == 401) {
        throw new IOException("Authentication failed");
    } else {
        throw new IOException(statusLine.getStatusCode() + ":"
                + statusLine.getReasonPhrase());
    }

    // Return the JSON Object
    return new JSONObject(stringBuilder.toString());
}

之后,您可以使用JSONObject类的方法检索数据,例如getInt(String)getString(String)。您可以使用getJSONObject(String) 获取嵌套对象。全部通过提供带有字段/对象名称的字符串作为参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多