【发布时间】:2015-08-03 18:35:03
【问题描述】:
我将 android 用户位置纬度和经度连同 registration ID 和其他参数作为 Asynctask(活动的嵌套类)中的 json 对象发送到服务器。但是 Location 对象(在应用程序开始时具有值)在活动中实例化,因此
location = LocationServices.FusedLocationApi
.getLastLocation(mgoogleapiclient);
在 Asynctask 类中返回 null。有人可以向我解释为什么吗?我是否必须使用单独的类来获取用户位置并将其发送到另一个异步任务或服务中(从架构的角度来看这没有意义)?
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
PostData pdata = new PostData();
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
pdata.execute(String.valueOf(latitude), String.valueOf(longitude));
}
我正在从 AsyncTask 类中检索这个:
json.put("latitude", String.valueOf(args[1]));
json.put("longitude", String.valueOf(args[2]));
但是当我调试它时,我得到了我从另一个方法发送到 AsyncTask 类的注册 ID。 我希望我能说清楚。
@Override
protected String doInBackground(String... args) {
try {
try {
URL url;
HttpURLConnection urlConn;
url = new URL ("myphp.php");
urlConn = (HttpURLConnection)url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type","application/json");
urlConn.setRequestProperty("Accept", "application/json");
urlConn.setChunkedStreamingMode(0);
urlConn.setRequestMethod("POST");
urlConn.connect();
//get google account
AccountManager am = AccountManager.get(getBaseContext()); // "this" references the current Context
Account[] accounts = am.getAccountsByType("com.google");
//Create JSONObject here
JSONObject json = new JSONObject();
json.put("regID", String.valueOf(args[0]));
json.put("Google account", accounts[0].name);
json.put("name", "Amanda");
json.put("tel", "2069994444");
json.put("latitude", String.valueOf(args[1]));
json.put("longitude", String.valueOf(args[2]));
String postData=json.toString();
// Send POST output.
OutputStreamWriter os = new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8");
os.write(postData);
Log.i("NOTIFICATION", "Data Sent");
os.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String msg="";
String line = "";
while ((line = reader.readLine()) != null) {
msg += line; }
Log.i("msg=",""+msg);
} catch (MalformedURLException muex) {
// TODO Auto-generated catch block
muex.printStackTrace();
} catch (IOException ioex){
ioex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("ERROR", "There is error in this code");
}
return null;
}
以下是我发送注册ID的方式
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (!regid.isEmpty()) {
PostData pd = new PostData();
pd.execute(regid);
} else {
//register
registerInBackground();
}
【问题讨论】:
-
getLastLocation()可能返回 null。它经常这样做。注册一个监听器,并从onLocationChanged()回调中调用您的 AsyncTask。 -
@Daniel 感谢您的回答,但到目前为止我还没有运气。我可以调用 Asynctask 两次以传递 ID 并从两种不同的方法传递纬度和经度吗?如何检索 doInBackground(String ...params) 方法中的值?
-
您是否在访问 AsycTask 中的 JSONObject 和 Location 对象时遇到问题?请发布您迄今为止尝试过的代码。您还可以在此处查看用于注册位置侦听器的示例代码:stackoverflow.com/questions/30191047/…
-
我已经更新了问题。
-
您是否在代码中的其他地方以不同的方式启动 AsyncTask?您可以添加使用注册 ID 调用它的代码,还可以添加整个 AsyncTask 代码吗?
标签: java android android-asynctask