我从位置管理器获取纬度和经度,但是它未与Thingboard HTTP物联网云连接。我使用了HTTP发布响应。它不会产生任何错误,但也不会传输任何数据。由于它是AsyncTask,因此不会产生任何错误。我已经对纬度和经度进行了编码
package com.example.currentgpslocation;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity<policy> extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
String GPS_PROVIDER;
String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
GPS_PROVIDER = locationManager.getBestProvider(criteria, false);
if (GPS_PROVIDER != null && !GPS_PROVIDER.equals("")) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);;
locationManager.requestLocationUpdates(GPS_PROVIDER, 15000, 1, this);
if (location != null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "No Location Provider Found Check Your Code", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLocationChanged(Location location) {
TextView longitude = (TextView) findViewById(R.id.textView);
TextView latitude = (TextView) findViewById(R.id.textView1);
longitude.setText("Current Longitude:" + location.getLongitude());
latitude.setText("Current Latitude:" + location.getLatitude());
try {
Object latitud = location.getLatitude();
latitud = latitud.toString();
Object longtitud = location.getLongitude();
longtitud = longtitud.toString();
data = URLEncoder.encode("latitude", "UTF-8")
+ "=" + URLEncoder.encode(String.valueOf(latitude), "UTF-8");
data += "," + URLEncoder.encode("longtitude", "UTF-8")
+ "=" + URLEncoder.encode(String.valueOf(latitude), "UTF-8");
}catch (Exception ex){
ex.printStackTrace();
}
DemoTask ob= new DemoTask();
ob.execute();
}
public class DemoTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... arg0) {
String text = "";
BufferedReader reader = null;
try {
// Defined URL where to send data
URL url = new URL("https://cloud.thingsboard.io/api/v1/JD98YCFBU8ywGIwVmdFL/telemetry");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.connect();
int responseCode = conn.getResponseCode();
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "UTF8");
wr.write(data);
conn.getErrorStream();
wr.flush();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
wr.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return null;
}
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
生成错误: W / System.err:java.io.IOException:已关闭 W / System.err:at com.android.okhttp.okio.RealBufferedSink $ 1.write(RealBufferedSink.java:196)