JSON string vom internet connection BoradcastReceiver schicken

T

the_time

Ambitioniertes Mitglied
0
Hallo,
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}
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
PHP:
                PostData sender = new PostData();
                sender.timer(jSONString);
                textJSON.setText(jSONString);
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?

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);
                  
    
    
                }
            }
        }
    }
 
Frage 1)
Warum machst du ein neues Thema auf? Ist doch immer noch das selbe Problem wie in deinem letztem Thema ?

Frage 2)
Was willst du mit einem BroadcastReceiver ? Mach dir ne methode, die prüft ob du Netz hast (hattest du nicht im anderen Thema schon eine ?) und klammere deine funktionierende Lösung im onLocationChanged in ein
Code:
if (isNetzDa()) {
    PostData sender = new PostData();
    sender.timer(jSONString);
    textJSON.setText(jSONString); 
} else {
    ...  User-info, das kein Netz da  ...
und alles gut ?
 
  • Danke
Reaktionen: the_time
Hallo,
ich dachte man muss ein neues Thema erstellen wenn die Frage anders ist.

Ja Danke für den Tipp :D ohne BoradcastReceiver ist besser.

Schöne Grüße,
the_time
 

Ähnliche Themen

W
Antworten
16
Aufrufe
844
jogimuc
J
benj98
  • benj98
Antworten
4
Aufrufe
1.274
benj98
benj98
H
  • HerrFatalVonError
Antworten
4
Aufrufe
788
jogimuc
J
Zurück
Oben Unten