T
the_time
Ambitioniertes Mitglied
- 0
Hallo,
ich will einen JSON string mit den folgenden Daten
vom BroadcastReceiverListener schicken also wenn die Internetverbindung vorhanden ist aber da drin ist mein JSON string null wie mache ich es damit er dort nicht null ist? Wenn ich diesen Teil
in onChangedLocation method einsetze läuft alles aber wie gesagt ich will den jSONString an Meine PostData klasse erst schicken wenn die Internet verbindung zur Verfügung steht?
ich will einen JSON string mit den folgenden Daten
PHP:
{"latitude":23.86898504,"longitude":12.66561187,"formatted":"25.04.2015 15:13:11","route":4}
PHP:
PostData sender = new PostData();
sender.timer(jSONString);
textJSON.setText(jSONString);
PHP:
public class MainActivity extends ActionBarActivity {
int route_number;
double pLong;
double pLat;
String time;
String jSONString;
TextView textJSON;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textJSON = (TextView) findViewById(R.id.textJSON);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, ll);
}
private class BroadcastReceiverListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
//code to get the strongest wifi access point in the JSON string the routes's value.
}
else if (intent.getAction().equals(
android.net.ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager
.getActiveNetworkInfo();
boolean isConnected = netInfo != null
&& netInfo.isConnectedOrConnecting();
if (isConnected) {
Toast.makeText(context,
"The device is connected to the internet ",
Toast.LENGTH_SHORT).show();
PostData sender = new PostData();
//here JSON string has null.
System.out.println("The output of internet broadcast: " +jSONString);
sender.timer(jSONString);
textJSON.setText(jSONString);
} else {
Toast.makeText(context,
"Please connect the device to the internet.",
Toast.LENGTH_SHORT).show();
}
}
}
}
class myLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
pLong = location.getLongitude();
pLat = location.getLatitude();
...
time = sdf.format(location.getTime());
jSONString = convertToJSON(pLong, pLat, time);
System.out.println("The output of onLocationChanged: "+ jSONString);
//The code works fine here. JSON string has its values here but in broadcastReceiver JSON string is getting null.
// PostData sender = new PostData();
// sender.timer(jSONString);
// textJSON.setText(jSONString);
}
}
}
}