1

私はC#を使用してWebMethod経由でJSONを作成するASP.NET Webサイトを持っています.Webメソッドは、httpポスト経由でJava Androidアプリケーションから呼び出されます。webmethod にページ ID を指定すると、ページのコンテンツが返されます。この場合、エラー ページのコンテンツが返されます。

これは、webmethod によって返される 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;
    }
}

resultsFromJson によって返される Containerdata は、webmethod によって返される json に対して何をしても常に 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 "";
    }
}

誰か助けて、私を正しい方向に向けてください。

よろしくお願いします!

4

1 に答える 1