java - Google GSON 不解析从 C# 网站中的 WebMethod 检索到的 Java 中的 JSON

标签 java android json gson webmethod

我有一个 ASP.NET 网站,它使用 C# 通过 WebMethod 创建 JSON,然后通过 http post 从 Java android 应用程序调用 webmethod。我为 web 方法提供了一个页面 ID,它返回页面内容,在本例中它返回错误页面的内容。

这是网络方法返回的 JSON:

D/WebInterface( 2353): {"d":[{"intId":2418,"strName":"Error 404","strTitle":"Ooo
ps, I couldn\u0027t find that!","strSummary":"Error 404, I couldn\u0027t find th
e page you\u0027re looking for.","strBody":"\u003cp\u003eYou could try browsing
the website or checking that you typed the URL correctly, if you came to this pa
ge from another site, please let me know and thanks for visiting.\u003c/p\u003e"
,"strUpdateDate":null,"strCreateDate":null}]}

我在我的 Android 应用程序中使用 Google GSON 从 JSON 创建一个对象,但无论我做什么它都会返回 null。这是我的 Google GSON 方法:

public static Containerdata resultsFromJson(String json)
{
    try
    {
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();

        Containerdata results = gson.fromJson(json, Containerdata.class);

        Log.d("WebInterface", "RETURNING OBJECT FROM JSON");

        return results;
    }
    catch(Exception e)
    {
        Log.d("WebInterface", "Error: Malformed JSON.");
        return null;
    }
}

此方法返回以下 Containerdata:

public class Containerdata {
    public List<Containerdata.Node> d;

    public class Node
    {
        int intId;
        String strName;
        String strTitle;
        String strSummary;
        String strBody;
        String strUpdateDate;
        String strCreatedate;
    }
}

无论我对 web 方法返回的 json 做什么,resultsFromJson 返回的 Containerdata 始终为 null,我不知道为什么。这是从我的 WebMethod 获取我的 JSON 的方法:

// Gets json in the form of a string from a web service
public static String dataFromWeb(String url, String postData)
{
    Log.d("WebInterface", "Loading from web");
    try
    {
        HttpURLConnection httpcon = (HttpURLConnection) ((new URL(url).openConnection()));
        httpcon.setDoOutput(true);
        httpcon.setRequestProperty("Content-Type", "application/json");
        httpcon.setRequestProperty("Accept", "application/json");
        httpcon.setRequestMethod("POST");
        httpcon.connect();

        byte[] outputBytes = postData.getBytes("UTF-8");
        OutputStream os = httpcon.getOutputStream();
        os.write(outputBytes);

        os.close();

        InputStream response = httpcon.getInputStream();

        Log.d("WebInterface", Helpers.convertStreamToString(response));

        return Helpers.convertStreamToString(response);
    }
    catch(Exception e)
    {
        Log.d("WebInterface", "failed from web... " + e.toString());

        return "";
    }
}

谁能帮我指出正确的方向,我将不胜感激。

提前致谢!

最佳答案

问题出在下面的代码中...

InputStream response = httpcon.getInputStream();

Log.d("WebInterface", Helpers.convertStreamToString(response));

return Helpers.convertStreamToString(response);

您基本上是在尝试读取 InputStream 两次。第一次是记录响应,第二次是尝试返回 响应。问题是您无法读取已读取的流(好吧,您可以,但这需要不同的代码)。

如果你想记录响应然后在本地读取字符串然后记录并返回它...

InputStream response = httpcon.getInputStream();

String responseString = Helpers.convertStreamToString(response);
Log.d("WebInterface", responseString);

return responseString;

关于java - Google GSON 不解析从 C# 网站中的 WebMethod 检索到的 Java 中的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15982073/

相关文章:

java - JdbcRowSet 返回 SQL 异常

java - Android DownloadManager 以原始名称保存文件

安卓CMake : 'uint64_t' does not name a type

c# - Json.NET:反序列化嵌套字典

json - 获取此输出 json 的所有值

javascript - 使用 Javascript 访问二维 JSON 数组

java - 使用 Spring 进行 MongoDB 复制

Java:使用代码模型生成具有默认值的注释

java - wolframalpha 和 commons-math 之间的快速傅里叶变换结果差异

android - 如何以编程方式在 sdcard 中创建一个可以直接在 Windows 和手机中显示的文件夹