Android: Post JSON 数据与本地主机 (WAMP) 的 HttpURLConnection

Android: Post JSON data with HttpURLConnection to the localhost(WAMP)

我正在尝试 post JSON 带有 HttpURLConnection 的字符串到本地主机服务器 (WAMP)。在我的方法中,我从计时器调用 AsyncTask class 但是当编译器到达这一行时我遇到了问题 handler.post(new Runnable() 它跳回到 while (run) 而不是输入 public void run() {new MyAsyncTask().execute(jSONString);}

我从 c​​onvertToJSON 方法得到的 JSON 字符串:

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

这部分代码在MainActivity内部class"MyLocationListener"的onLocationChanged方法中:

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

邮政数据class:

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;

        }

    }

}

由于您已经在使用 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