ASP.NET JSON Web Service Response format

前端 未结 4 1617
没有蜡笔的小新
没有蜡笔的小新 2020-12-11 17:37

I have written one simple web service which get product list in JSONText which is string object

Web Service code is below

using System;
using Syste         


        
4条回答
  •  隐瞒了意图╮
    2020-12-11 18:40

    in .net web service

    [WebMethod]
    public string Android_DDD(string KullaniciKey, string Durum, string PersonelKey)
    {
        return EU.EncodeToBase64("{\"Status\":\"OK\",\"R\":[{\"ImzaTipi\":\"Paraf\", \"Personel\":\"Ali Veli üğişçöıÜĞİŞÇÖI\", \"ImzaDurumTipi\":\"Tamam\", \"TamamTar\":\"1.1.2003 11:21\"},{\"ImzaTipi\":\"İmza\", \"Personel\":\"Ali Ak\", \"ImzaDurumTipi\":\"Tamam\", \"TamamTar\":\"2.2.2003 11:21\"}]}");
    }
    
    static public string EncodeToBase64(string toEncode)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] bytes = encoding.GetBytes(toEncode);
        string returnValue = System.Convert.ToBase64String(bytes);
        return returnValue;
    }
    

    in android

    private static String convertStreamToString(InputStream is)
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try
        {
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                is.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    
    private void LoadJsonDataFromASPNET()
    {
        try
        {              
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPostRequest = new HttpPost(this.WSURL + "/WS.asmx/Android_DDD");
            JSONObject jsonObjSend = new JSONObject();
            jsonObjSend.put("KullaniciKey", "value_1");
            jsonObjSend.put("Durum", "value_2");
            jsonObjSend.put("PersonelKey", "value_3");
            StringEntity se = new StringEntity(jsonObjSend.toString());
            httpPostRequest.setEntity(se);
            httpPostRequest.setHeader("Accept", "application/json");
            httpPostRequest.setHeader("Content-type", "application/json");
            // httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression
    
            HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
            HttpEntity entity = response.getEntity();
            if (entity != null)
            {
                InputStream instream = entity.getContent();
                String resultString = convertStreamToString(instream);
                instream.close();
                resultString = resultString.substring(6, resultString.length()-3);
                resultString = new String(android.util.Base64.decode(resultString, 0), "UTF-8");
                JSONObject object = new JSONObject(resultString);
                String oDurum = object.getString("Status");
                if (oDurum.equals("OK"))
                {
                    JSONArray jsonArray = new JSONArray(object.getString("R"));
                    if (jsonArray.length() > 0)
                    {
                        for (int i = 0; i < jsonArray.length(); i++)
                        {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            String ImzaTipi = jsonObject.getString("ImzaTipi");
                            String Personel = jsonObject.getString("Personel");
                            String ImzaDurumTipi = jsonObject.getString("ImzaDurumTipi");
                            String TamamTar = jsonObject.getString("TamamTar");
                            Toast.makeText(getApplicationContext(), "ImzaTipi:" + ImzaTipi + " Personel:" + Personel + " ImzaDurumTipi:" + ImzaDurumTipi + " TamamTar:" + TamamTar, Toast.LENGTH_LONG).show();
                        }   
                    }
                }   
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题