如何在Android服务中使用ajax?

时间:2017-02-18 16:26:49

标签: java android ajax cordova cordova-plugins

我希望服务在服务器上保留请求PHP文件,并在返回值发生更改时查看通知 我之前从未在java工作,任何人都可以帮助我吗?

我搜索了java的ajax代码,我发现了这个,但是当我构建应用程序时显示错误:

package com.red_folder.phonegap.plugin.backgroundservice.sample;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;

public class MyService extends BackgroundService {

    private final static String TAG = MyService.class.getSimpleName();

    private String mHelloTo = "World";

    @Override
    protected JSONObject doWork() {
        JSONObject result = new JSONObject();

        URL url = new URL("http://192.168.0.106/smarthome/value.php");
        URLConnection connection = url.openConnection();
        connection.connect();

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;

        while((inputLine = in.readLine()) != null)
        {
            result.put("Message", inputLine);
        }

        in.close();

        return result;  
    }

    @Override
    protected JSONObject getConfig() {
        JSONObject result = new JSONObject();

        try {
            result.put("HelloTo", this.mHelloTo);
        } catch (JSONException e) {
        }

        return result;
    }

    @Override
    protected void setConfig(JSONObject config) {
        try {
            if (config.has("HelloTo"))
                this.mHelloTo = config.getString("HelloTo");
        } catch (JSONException e) {
        }

    }     

    @Override
    protected JSONObject initialiseLatestResult() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onTimerEnabled() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onTimerDisabled() {
        // TODO Auto-generated method stub

    }


}

我正在使用此插件用于cordova: https://github.com/Red-Folder/bgs-core

0 个答案:

没有答案