2015-04-22 32 views
0

我想发布JSON字符串与HttpURLConnection本地主机服务器(WAMP)。在我的方法,我从计时器调用的AsyncTask类,但是当handler.post(new Runnable()它跳回while (run),而不是进入public void run() {new MyAsyncTask().execute(jSONString);}Android:发布JSON数据与HttpURLConnection本地主机(WAMP)

的JSON字符串,我从convertToJSON方法让编译器到达这条线,我面临的问题:

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

这部分代码是在在MainActivity的内部类 “MyLocationListener” 的onLocationChanged方法:

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; 

     } 

    } 

} 
+0

快问,为什么处理?难道你不能在sleep语句之后直接执行asynctask吗? –

+0

新的处理程序和新的MyAsyncTask需要在android应用程序的主线程中实例化。尝试执行新的MyAsyncTask()。execute(jSONString);从一个按钮单击并将Thread.sleep插入到doInBackground方法的开始处... –

+0

@Markus:我不需要该按钮,但这是否意味着我必须将该线程放在doInBackground方法中? –

回答

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

+0

我试过你的代码,但它总是粘在这里'handler.postDelayed(new Runnable(){' –