简体   繁体   English

试图找出通过Android将数据发送到服务器的最佳方法

[英]Trying to figure out the best way to send data to a server via android

I'm trying to figure out what the best method to send two strings and an integer to a server. 我正在尝试找出将两个字符串和一个整数发送到服务器的最佳方法。 Should I use a database? 我应该使用数据库吗? I want to achieve a queue effect and not really store the data online for too long. 我想实现队列效果,而不是将数据在线存储太长时间。 I just want to grab the information in the queue with a different program. 我只想使用其他程序来获取队列中的信息。 The android would just need to send the data to the web server, but I'm not sure what kind of data structures I should be researching. android只需要将数据发送到Web服务器,但是我不确定应该研究哪种数据结构。

So I'm really looking for ideas of some kind of scripts I can be running on a web-server and how to implement sending to them via android. 所以我真的在寻找可以在网络服务器上运行的某种脚本的想法,以及如何实现通过android发送给它们的想法。 I don't need specific code, but an idea I could research would work too. 我不需要特定的代码,但是我可以研究的想法也可以。 I really appreciate any help. 我非常感谢您的帮助。

Simple, Encode what you need to send in JSON and send it. 简单,将需要发送的内容编码为JSON并发送。 I'll quote an example below, I have a server running Django and have set up an API to save the data into the database when I hit the url with a post request. 我在下面引用一个示例,我有一台运行Django的服务器,并设置了一个API,当我用发布请求访问url时将数据保存到数据库中。 I send the Location (latitude and longitude) along with the current datetime. 我发送位置(纬度和经度)以及当前日期时间。 On my server a view runs and parses the json and then saves it to DB. 在我的服务器上,运行视图并解析json,然后将其保存到DB。

In your case however you can just manipulate the data the way you want and not save it. 但是,在您的情况下,您可以仅按所需方式操作数据,而不保存数据。

    try 
    {
            double la = 30.1955600 ;
            double lo = 71.4752800;
            Time t = new Time(Time.getCurrentTimezone());
            t.setToNow();
            String date = t.format("%Y-%m-%d %H:%M:%S");


            JSONObject o1 = null;                                                               
            o1 = new JSONObject();
            o1.put("latitude", loc.latitude);
            o1.put("longitude", loc.longitude);
            o1.put("date_added", date);




            // http call//
            HttpClient httpClient = new DefaultHttpClient();

            HttpPost request = new HttpPost("the url you want to hit");

            StringEntity _params =new StringEntity(o1.toString());

            request.addHeader("content-type", "application/json");

            request.setEntity(_params);
            HttpResponse response = httpClient.execute(request);    
    //check to see if we got a response

            if (response.getEntity() != null)
                HomeScreen.location_sent = true;
            else
                HomeScreen.location_sent = false;

    }
    catch (Exception e) 
    {
        e.getMessage();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在一次传输中将数据/文件发送到Android中服务器的最佳方法 - Best way to send data/files in one transmission to server in android 在android中将大量事件发送到服务器的最佳方式 - Best way to send lots of events to server in android 从数据库向Android发送数据的最佳方法 - Best way to send data from DB to Android 通过设备发送数据的最佳方法? (Android) - Best way to send data through devices? (Android) 拥有推送通知服务器并在android设备和服务器之间发送数据的最佳方法是什么? - What is the best way to have a push notification server and send data between android device and server? 从android->服务器->另一个android实时发送数据的最佳方式 - best way to send data from android->server->another android in realtime 通过 JSON 从 android 向服务器发送数据 - Send data from android to server via JSON 确定先前是否选择过EditText的最佳方法 - Best way to figure out if a EditText was previously selected 使用Java从Android向Web服务器发送和接收数据(POST和GET)的最佳方式? - Best way to send and receive data(POST and GET) from Android to web server using Java? 试图找出这个Android网络问题 - Trying to figure out this Android Networking issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM