Android:将JSON字符串传递给服务class"PostData"

Android: Pass JSON string to the service class "PostData"

我正在尝试借助 HttpURLConnection api 将数据 post 发送到本地主机服务器 (WAMP)。我在 IntentService 的帮助下实现了它,但我不知道如何将数据 "JSON string" 从 MainActivity 传递到 DataPost class.

这部分是从 MainActivity 的内部 class "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);

邮政数据class:

   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;
import android.os.AsyncTask;
import android.os.Handler;

public class PostData extends IntentService {
    String jSONString;
    Handler handler = new Handler();

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

    }

    public String getjSONString() {
        return jSONString;
    }

    public void setjSONString(String jSONString) {
        this.jSONString = jSONString;
    }

    @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();
        }

    }

}

I do not know how can I pass the data "JSON string" from MainActivity to the DataPost class

使用 Intent.putExtrajSONString 字符串传递给 IntentService :

....
// add jSONString to intent using any key
intent3.putExtra("json_data",jSONString);
startService(intent3);

现在在onHandleIntent中使用intent.getStringExtra得到字符串:

@Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
     String jSONString = intent.getStringExtra("json_data");
    }

注意:不需要在IntentService中使用AsyncTask,因为onHandleIntent方法是在工作线程上调用的请求过程

所以使用onHandleIntent方法进行http请求

你可以这样使用putExtra,

Intent i = new Intent(getApplicationContext(), PostData.class);
x="3";
y="A";
i.putExtra(ValueX, x);
i.putExtra(ValueY, y);
startActivity(i);

看看客户端会怎么接收并尝试解析,应该不难。