T
the_time
Ambitioniertes Mitglied
- 0
Hallo,
ich versuche gerade JSON string an die PostData klasse von - onLocationChanged() der inneren Klasse "MyLocationListener" in der MainActivity- zu übergeben mithilfe von Intent aber ich kriege null für "jSONString" in der onHandleIntent() methode. weißt jemand was noch fehlt um es hinzubekommen?
Das sind die Daten in der Variable "String jSONString = convertToJSON(pLong, pLat, formatted);" drin:
Dieser Teil vom Code wird in der onLocationChanged()- MyLocationListener (inner class)- MainActivity aufgerufen:
PostData Klasse:
ich versuche gerade JSON string an die PostData klasse von - onLocationChanged() der inneren Klasse "MyLocationListener" in der MainActivity- zu übergeben mithilfe von Intent aber ich kriege null für "jSONString" in der onHandleIntent() methode. weißt jemand was noch fehlt um es hinzubekommen?
Das sind die Daten in der Variable "String jSONString = convertToJSON(pLong, pLat, formatted);" drin:
PHP:
{"latitude":50.86898824,"longitude":15.66546405,"formatted":"23.04.2015 16:36:11","route":4}
PHP:
String jSONString = convertToJSON(pLong, pLat, formatted);
Intent intent3 = new Intent(MainActivity.this, PostData.class);
intent3.putExtra("json_data", jSONString);
PendingIntent pintent3 = PendingIntent.getService(getApplicationContext(), 0, intent3, 0);
AlarmManager alarm3 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
// for 30 mint 60*60*1000
alarm3.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
1000, pintent3);
startService(intent3);
PHP:
public class PostData extends IntentService {
public PostData() {
super("some");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
String jSONString = intent.getStringExtra("json_data");
try {
// This is the ip address of my laptop wifi because I am running the
// app in my device and I want to send the data to the localhost
// server(WAMP).
URL myUrl = new URL("http://192.168.x.x/webservice");
HttpURLConnection myConnection = (HttpURLConnection) myUrl
.openConnection();
myConnection.setRequestMethod("POST");
myConnection.setDoOutput(true);
myConnection.setUseCaches(false);
myConnection.setConnectTimeout(10000);
myConnection.setReadTimeout(10000);
myConnection.setRequestProperty("Content-Type", "application/json");
myConnection.connect();
// create data output stream
DataOutputStream wr = new DataOutputStream(
myConnection.getOutputStream());
// write to the output stream from the string
wr.writeBytes(jSONString);
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}