Tuesday, May 8, 2018

android - Tablet (4.0.3) stops posting to server on launch while phone (2.2.3) works

I figured out that my tablet with android 4.0.3 stops here while my phone with 2.2.3 works on this code but I do not know why:


I can not think of a difference between the tablet and phone coding, someone?


/* Store this device macaddress within the server. */
static void storeMacAddress(final Context context, final String macAddr) {
Log.i(TAG, "Store device on Server (macAddress = " + macAddr + ")");
String serverUrl = SERVER_URLL;
Map params = new HashMap();
params.put("macaddress", macAddr);
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
// As the server might be down, we will retry it a couple times.
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
Log.d(TAG, "Attempt #" + i + " to store");
try {
post(serverUrl, params);
Log.d(TAG, "Store op Server gelukt!");
return;
} catch (IOException e) {
// Here we are simplifying and retrying on any error
Log.e(TAG, "Failed to store on attempt " + i + ":" + e);
if (i == MAX_ATTEMPTS) {
break;
}
try {
Log.d(TAG, "Sleeping for " + backoff + " ms before retry");
Thread.sleep(backoff);
} catch (InterruptedException e1) {
// Activity finished before we complete - exit.
Log.d(TAG, "Thread interrupted: abort remaining retries!");
Thread.currentThread().interrupt();
return;
}
// increase backoff exponentially
backoff *= 2;
}
}
Log.d(TAG, "Error tijdens store op Server procedure!");
}
private static void post(String endpoint, Map params)
throws IOException {
URL url;
try {
url = new URL(endpoint);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("invalid url: " + endpoint);
}
StringBuilder bodyBuilder = new StringBuilder();
Iterator> iterator = params.entrySet().iterator();
// constructs the POST body using the parameters
while (iterator.hasNext()) {
Entry param = iterator.next();
bodyBuilder.append(param.getKey()).append('=')
.append(param.getValue());
if (iterator.hasNext()) {
bodyBuilder.append('&');
}
}
String body = bodyBuilder.toString();
Log.v(TAG, "Posting '" + body + "' to " + url);
byte[] bytes = body.getBytes();
HttpURLConnection conn = null;
try {
Log.e("URL", "> " + url);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
// post the request
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
// handle the response
int status = conn.getResponseCode();
if (status != 200) {
throw new IOException("Post failed with error code " + status);
}
} finally {
if (conn != null) {
conn.disconnect();
}
}
}

EDIT:
Calling from:


...
setContentView(R.layout.activity_main);
final SharedPreferences prefs = this.getSharedPreferences("nl.easy.winkel", Context.MODE_PRIVATE);
if(!prefs.getString("macaddress","").equals("send")) { // Send MacAddress once
prefs.edit().putString("macaddress","send").commit();
obtainMacAddress();
}
and
private void obtainMacAddress() {
WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String macAddr = wifiInf.getMacAddress();
final Context context = this;
ServerUtilities.storeMacAddress(context, macAddr + "|" + getLocalIpAddress());
}
public String getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(TAG, ex.toString());
}
return null;
}

EDIT2: using AsyncTask


AsyncTask mStoreTask;
private void obtainMacAddress() {
WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
final String macAddr = wifiInf.getMacAddress();
final Context context = this;
mStoreTask = new AsyncTask() {
@Override
protected Void doInBackground(Void... params) {
// Store on our server
// On server creates a new user
ServerUtilities.storeMacAddress(context, macAddr + "|" + getLocalIpAddress());
return null;
}
@Override
protected void onPostExecute(Void result) {
mStoreTask = null;
}
};
mStoreTask.execute(null, null, null);
}

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...