1

我有一个 ASP.NET 网站,它使用 C# 通过 WebMethod 创建 JSON,然后通过 http post 从 Java android 应用程序调用 webmethod。我为 webmethod 提供了一个页面 id,它返回页面内容,在这种情况下,它返回错误页面的内容。

这是 web 方法返回的 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。这是我的谷歌 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;
    }
}

无论我对 webmethod 返回的 json 做什么,resultsFromJson 返回的 Containerdata 始终为空,我不知道为什么。这是从我的 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 "";
    }
}

任何人都可以帮助并指出正确的方向,我会非常感激。

提前非常感谢!

4

1 回答 1

1

问题出在以下代码中...

InputStream response = httpcon.getInputStream();

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

return Helpers.convertStreamToString(response);

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

如果要记录响应,请在本地读取字符串,然后记录并返回...

InputStream response = httpcon.getInputStream();

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

return responseString;
于 2013-04-12T23:55:32.887 回答