Android:使用HttpURLConnection将JSON数据发布到localhost(WAMP)

时间:2015-04-22 20:26:04

标签: android android-asynctask httpurlconnection

我正在尝试将带有HttpURLConnection的JSON字符串发布到localhost服务器(WAMP)。在我的方法中,我从计时器调用AsyncTask类,但是当编译器到达此行handler.post(new Runnable()时,我将面临问题,它会跳回到while (run)而不是输入public void run() {new MyAsyncTask().execute(jSONString);}

我从convertToJSON方法得到的JSON字符串:

{ 
    "formatted" : "22.04.2015 11:11:00",
    "latitude" : 53.869073210000003,
    "longitude" : 10.66542435,
    "route" : 4
}

这部分代码位于内部类" MyLocationListener"中的onLocationChanged方法中。 MainActivity:

String jSONString = convertToJSON(pLong, pLat, formatted);
PostData sender = new PostData(jSONString);
sender.timer();

PostData类:

package com.bustracker;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.AsyncTask;
import android.os.Handler;

public class PostData {
    String jSONString;
    Handler handler = new Handler();

    public PostData(String jSONString) {
        super();
        this.jSONString = jSONString;
    }

    public String getjSONString() {
        return jSONString;
    }

    public void setjSONString(String jSONString) {
        this.jSONString = jSONString;
    }

    public void timer() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                boolean run = true;
                while (run) {
                    try {
                        Thread.sleep(5000);// 60000 milliseconds which is 60
                                            // seconds

                        handler.post(new Runnable() {

                            @Override
                            public void run() {
                                // here you send data to server
                                new MyAsyncTask().execute(jSONString);
                            }
                        });
                    } catch (Exception e) {
                        run = false;
                    }
                }
            }
        }).start();
    }

    class MyAsyncTask extends AsyncTask<String, Integer, Void> {

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub

            try {
                //This is the ip address of my laptop wifi because I am running the app in my device and I want to send the data to the localhost server(WAMP). 
                URL myUrl = new URL("http://192.168.X.X/webservice"); 
                HttpURLConnection myConnection = (HttpURLConnection) myUrl
                        .openConnection();
                myConnection.setRequestMethod("POST");
                myConnection.setDoOutput(true);
                myConnection.setUseCaches(false);
                myConnection.setConnectTimeout(10000);
                myConnection.setReadTimeout(10000);
                myConnection.setRequestProperty("Content-Type",
                        "application/json");
                myConnection.connect();
                // create data output stream
                DataOutputStream wr = new DataOutputStream(
                        myConnection.getOutputStream());
                // write to the output stream from the string
                wr.writeBytes(jSONString);
                wr.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
            return null;

        }

    }

}

1 个答案:

答案 0 :(得分:1)

由于您已经在使用Handler,因此可以将其用作计时器。

public void timer() {

    new Thread(new Runnable() {
        @Override
        public void run() {
            boolean run = true;
            while (run) {
               handler.postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       new MyAsyncTask().execute(jSONString);
                   }
               }, 5000);
            }
        }
    }).start();
}

供参考,请访问http://binarybuffer.com/2012/07/executing-scheduled-periodic-tasks-in-android