Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:将数据从MainActivity中的内部类传递到服务类_Android_Android Service - Fatal编程技术网

Android:将数据从MainActivity中的内部类传递到服务类

Android:将数据从MainActivity中的内部类传递到服务类,android,android-service,Android,Android Service,我正试图借助HttpUrlConnectionAPI将数据发布到本地主机服务器(WAMP)。我在IntentService的帮助下实现了它。目前我面临的问题是,jSONString在onhandlecontent中一无所获。 谢谢你的帮助 正在从MainActivity中内部类“MyLocationListener”中的onLocationChanged方法调用此部件: String jSONString = convertToJSON(pLong, pLat, formatted);

我正试图借助HttpUrlConnectionAPI将数据发布到本地主机服务器(WAMP)。我在IntentService的帮助下实现了它。目前我面临的问题是,
jSONString
onhandlecontent
中一无所获。 谢谢你的帮助

正在从MainActivity中内部类“MyLocationListener”中的onLocationChanged方法调用此部件:

String jSONString = convertToJSON(pLong, pLat, formatted);      
                Intent intent3 = new Intent(MainActivity.this, PostData.class);
                intent3.putExtra("json_data", jSONString);
                PendingIntent pintent3 = PendingIntent.getService(getApplicationContext(), 0, intent3, 0);
                AlarmManager alarm3 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Calendar cal = Calendar.getInstance();
                // for 30 mint 60*60*1000
                alarm3.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                        1000, pintent3);
                startService(intent3);
PostData类:

package com.bustracker;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.IntentService;
import android.content.Intent;


public class PostData extends IntentService {


    public PostData() {
        super("some");

    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        String jSONString = intent.getStringExtra("json_data");
        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();
        }

    }

}

如果尝试在onStartCommand()中读取数据,会发生什么情况?(基本上,只需将onHandleIntent()中的代码移到onStartCommand()中,我移动了它,但仍然会得到null!好吧,这是一个很长的过程。它应该在onHandleIntent中工作。我会有另一个想法。好吧,我已经将它移回:)但是你不知道为什么我在onHandleIntent中的jSONString会得到null吗?什么都没有通过!您是否绝对确定convertToJSON()不会返回null?