簡體   English   中英

使用JSON響應從Java調用AC#Web服務

[英]Calling a c# webservice from Java with JSON response

在工作的編程環境中,我們同時有Java和C#開發人員。 我有一個用C#創建的Web服務,Java開發人員正在嘗試使用該服務。 我一直在編寫Java來使用此Web服務,而當我得到json結果時,它的格式錯誤。

這是我在C#方面所擁有的:

[WebMethod]
public static LinkedList<string> GetInfo(string InfoID, string Username, string Password)
{
    LinkedList<string> Result = new LinkedList<string>();
    try
    {
        // Do some stuff I can't show you to get the information...
        foreach (Result from data operations)
        {
            Result.AddLast(sample1);
            Result.AddLast(sample2);
            Result.AddLast(sample3);
            Result.AddLast(BD));
            Result.AddLast(CN);
            Result.AddLast(Name);
            Result.AddLast("###");
        }
    }catch(Exception exc)
    {
        Result.AddLast(exc.ToString());
        return Result;
    }            
    return Result;
}

然后是Java Side:

try {
    String uri = "http://example.com/service.asmx/GetInfo";

    URL url = new URL(uri);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    // Setup Connection Properties
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("charset", "utf-8");
    connection.setRequestProperty("Accept", "application/json");            
    connection.setChunkedStreamingMode(0);
    connection.connect();

    // Create the JSON Going out
    byte[] parameters = "{'InfoID':'123456789','Username':'usernametoken','Password':'passwordtoken'}".getBytes("UTF-8");


    // Start doing stuff                
    DataOutputStream os = new DataOutputStream(connection.getOutputStream());
    os.write(parameters);
    os.close();         
    InputStream response;                   

    // Check for error , if none store response
    if(connection.getResponseCode() == 200){response = connection.getInputStream();}
    else{response = connection.getErrorStream();}

    InputStreamReader isr = new InputStreamReader(response);
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(isr);
    String read = br.readLine();

    while(read != null){
        sb.append(read);
        read = br.readLine();
    }   
    // Print the String     
    System.out.println(sb.toString());

    // Creat JSON off of String
    JSONObject token = new JSONObject(sb.toString());

    // print JSON
    System.out.println("Tokener: " + token.toString());
    response.close();

} catch(IOException exc) {
    System.out.println("There was an error creating the HTTP Call: " + exc.toString());
}

我得到的回應就是這種形式...

{"d":["Sample1","Sample2","Sample3","BD","CN","Name","###","Sample1","Sample2","Sample3","BD","CN","Name","###","Sample1","Sample2","Sample3","BD","CN","Name","###"]}

我想知道是否有更好的方法來發送響應,使得JSON看起來像這樣:

{"1":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"2":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"3":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"4":["Sample1","Sample2","Sample3","BD","CN","Name","###"]}

好的,我想我在這里看到您的問題。 您希望將數據序列化為

{"1":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"2":["Sample1","Sample2","Sample3","BD","CN","Name","###"],"3":["Sample1","Sample2","Sample3","BD","CN","Name","###"] ... etc

然而,您要序列化的數據結構是單個鏈接列表,這就是為什么將其序列化為單個長列表的原因。 您需要做的是更改數據結構。 Dictionary將是完美的,因為它很容易序列化為JSON。

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public static Dictionary<int,LinkedList<string>> GetInfo(string InfoID, string Username, string Password)
{
    var Result = new Dictionary<int,LinkedList<string>>();
    try
    {
        // Do some stuff I can't show you to get the information...

        foreach (Result from data operations)
        {
            var newList = new LinkedList<string>();     
            newList.AddLast(sample1);
            newList.AddLast(sample2);
            newList.AddLast(sample3);
            newList.AddLast(BD));
            newList.AddLast(CN);
            newList.AddLast(Name);
            newList.AddLast("###");
            int number = something //the number before the list
            Result.add( number, newList);
        }
    }catch(Exception exc)
    {
        .
        .
        .
    }            
    return Result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM