Java 安卓工作室:穿越经纬度
我的应用程序是一个天气api,用于收集当前位置的天气信息。我通过输入一个邮政编码来实现这一点,然后异步收集信息。我还使用GooglePlayServices查找通过经度和纬度发送到设备的位置。我想做的是将经度和纬度的值传递到我的API类中,并将其附加到RESTAPI url以查找信息。这很难解释,但我的代码可能会清楚地说明这一点 REST API和GooglePlayServices的代码:Java 安卓工作室:穿越经纬度,java,android,google-maps,android-intent,android-bundle,Java,Android,Google Maps,Android Intent,Android Bundle,我的应用程序是一个天气api,用于收集当前位置的天气信息。我通过输入一个邮政编码来实现这一点,然后异步收集信息。我还使用GooglePlayServices查找通过经度和纬度发送到设备的位置。我想做的是将经度和纬度的值传递到我的API类中,并将其附加到RESTAPI url以查找信息。这很难解释,但我的代码可能会清楚地说明这一点 REST API和GooglePlayServices的代码: import android.app.Activity; import android.content.
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.*;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.model.LatLng;
public class RESTAPI extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private GoogleApiClient mGoogleApiClient;
public static final String TAG = RESTAPI.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restapi);
// start the AsyncTask for calling the REST service using httpConnect class
new AsyncTaskParseJson().execute();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
// added asynctask class methods below - you can make this class as a separate class file
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
Bundle bundle = getIntent().getParcelableExtra("bundle");
LatLng latLng = bundle.getParcelable("latLng");
// json test string
String jsonTest;
// set the url of the web service to call
String yourServiceUrl = "http://api.apixu.com/v1/current.json?key=e87e62510df946cc84c02652162112&q=" + latLng ;
@Override
// this method is used for......................
protected void onPreExecute() {
}
@Override
// this method is used for...................
protected String doInBackground(String... arg0) {
try {
// create new instance of the httpConnect class
httpConnect jParser = new httpConnect();
// get json string from service url
String json = jParser.getJSONFromUrl(yourServiceUrl);
// save returned json to your test string
jsonTest = json.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
// below method will run when service HTTP request is complete
protected void onPostExecute(String strFromDoInBg) {
TextView tv1 = (TextView) findViewById(R.id.jsontext);
tv1.setText(jsonTest);
}
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Location services connected.");
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
// Blank for a moment...
} else {
handleNewLocation(location);
}
}
private void handleNewLocation(Location location) {
Intent i= new Intent(this, AsyncTaskParseJson.class);
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
Log.d(TAG, location.toString());
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
Bundle args = new Bundle();
args.putParcelable("latLng", latLng);
i.putExtra("bundle", args);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Location services suspended. Please reconnect.");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
@Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
}
}
现在我要在我的异步类中声明:
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
Bundle bundle = getIntent().getParcelableExtra("bundle");
LatLng latLng = bundle.getParcelable("latLng");
// json test string
String jsonTest;
// set the url of the web service to call
String yourServiceUrl = "http://api.apixu.com/v1/current.json?key=e87e62510df946cc84c02652162112&q=" + latLng ;
公共类AsyncTaskParseJson扩展AsyncTask{
Bundle Bundle=getIntent().getParcelableExtra(“Bundle”);
LatLng LatLng=bundle.getParcelable(“LatLng”);
//json测试字符串
字符串jsonTest;
//设置要调用的web服务的url
字符串yourServiceUrl=”http://api.apixu.com/v1/current.json?key=e87e62510df946cc84c02652162112&q=“+拉丁语;
结果是我的应用程序在选择活动时崩溃。我只想把这些长值和lat值带过去,并将它们添加到URL的末尾。我希望这是清楚的,对于这个混乱的类,任何建议和更好的结构都会得到感谢。谢谢
12-21 07:04:02.262 3020-3020/com.example.mr_br.healthandfitness应用程序E/AndroidRuntime:致命异常:
主要
流程:com.example.mr_br.healthandfitness应用程序,PID:3020
java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.mr_br.healthandfitnessapplication/com.example.mr_br.healthandfitnessapplication.RESTAPI}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)”
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
在android.app.ActivityThread.access$800(ActivityThread.java:151)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)上
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5254)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)”
在com.example.mr_br.healthandfitnessapplication.RESTAPI$AsyncTaskParseJson.(RESTAPI.java:46)
位于com.example.mr_br.healthandfitnessapplication.RESTAPI.onCreate(RESTAPI.java:34)
位于android.app.Activity.performCreate(Activity.java:5990)
位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
在android.app.ActivityThread.access$800(ActivityThread.java:151)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)上
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5254)
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
Bundle bundle = getIntent().getParcelableExtra("bundle");
LatLng latLng = bundle.getParcelable("latLng");
// json test string
String jsonTest;
// set the url of the web service to call
String yourServiceUrl = "http://api.apixu.com/v1/current.json?key=e87e62510df946cc84c02652162112&q=" + latLng ;