Java 在Android中,每10秒从服务器获取一次响应

Java 在Android中,每10秒从服务器获取一次响应,java,android,string,android-service,android-volley,Java,Android,String,Android Service,Android Volley,我正在创建一个服务,每10秒从服务器获得一次响应。在下面的代码中,我每10秒发出一个请求。但是,即使服务每10秒运行一次,Toast也显示“失败”。我的代码有什么问题 package gift.charge.free.mci.ir.freecharge; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import a

我正在创建一个
服务
,每10秒从服务器获得一次响应。在下面的代码中,我每10秒发出一个请求。但是,即使
服务
每10秒运行一次,
Toast
也显示“失败”。我的代码有什么问题

package gift.charge.free.mci.ir.freecharge;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;
import com.android.volley.toolbox.*;
import com.android.volley.VolleyError;
import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.Response;
import com.android.volley.Request;
import com.android.volley.Network;

public class GooglePlayService extends Service {
    public GooglePlayService() {
}
private Handler cHandler=new Handler();
@Override
public int onStartCommand(Intent intent, int flags, int startId){
    LetsUpdate();
    return START_STICKY;
}
private void LetsUpdate(){
    cHandler.postDelayed(UpdateMe,10000);
}
private Runnable UpdateMe=new Runnable() {
    @Override
    public void run() {
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url ="http://google.com";
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
            }
        });
        queue.add(stringRequest);
        sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
        LetsUpdate();
    }
};
@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}
}
package gift.charge.free.mci.ir.free;
导入android.app.Service;
导入android.content.Intent;
导入android.os.Handler;
导入android.os.IBinder;
导入android.widget.Toast;
导入com.android.volley.toolbox.*;
导入com.android.volley.VolleyError;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response.Listener;
导入com.android.volley.Response;
导入com.android.volley.Request;
导入com.android.volley.Network;
公共类GooglePlayService扩展服务{
谷歌公共游戏服务(){
}
私有处理程序cHandler=新处理程序();
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
LetsUpdate();
返回开始时间;
}
私有void LetsUpdate(){
后延迟(更新,10000);
}
private Runnable UpdateMe=new Runnable(){
@凌驾
公开募捐{
RequestQueue=Volley.newRequestQueue(getApplicationContext());
字符串url=”http://google.com";
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT.show();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(getApplicationContext(),“fail”,Toast.LENGTH\u SHORT.show();
}
});
添加(stringRequest);
sendBroadcast(新意图(意图.动作\关闭\系统\对话框));
LetsUpdate();
}
};
@凌驾
公共IBinder onBind(意向){
//TODO:将通信通道返回到服务。
抛出新的UnsupportedOperationException(“尚未实现”);
}
}
您的舱单中是否声明了正确的舱单

android.permission.INTERNET