如何使用Retrofit从服务器发布请求?

时间:2017-03-10 05:33:51

标签: android retrofit

这是我的网址http://192.168.1.217:8088/api/frontend/web/index.php?r=api/sms/send,我需要一些参数,以下是参数

    {"body":{"param":"VjvBNGoYQ3/nCytxN0zx5rr+6sewp7FABok5N3DdDdD0WWu7KCCohA=="},
"header":{"deviceId":"","appToken":"","serviceCode":"sms","appType":1,"appVersion":1,"clientType":1}}

我是怎么写代码使用改造的?谢谢高级。

1 个答案:

答案 0 :(得分:0)

这是我迄今为止如何使用改造的最好例子http://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/ 但是,如果你想要另一个例子,那就是我用于OpenWeatherMap的代码。

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
using System.Net;
using System.Runtime.Serialization;
using System.IO;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            TestApp testapp = new TestApp();
            var urls = testapp.GetURls();

            //get string from urls

            foreach (var url in urls)
            {
                var responseString=testapp.GetURLString(url);

                //now save response into database
                testapp.saveResponseToDB(responseString);
            }
        }
    }

    public class TestApp
    {
        public void saveResponseToDB(string response)
        {
            //save into db
        }
        public List<string> GetURls()
        {
            var urls =new List<string>();

            //fetching more then 100000 urls from database.

            return urls;
        }

        public string GetURLString(string requestUrl)
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // return the content.
                return responseFromServer;
            }
        }
    }
}
相关问题