Created
June 16, 2015 13:25
-
-
Save siscodev93/bacaf28c65d5f4801764 to your computer and use it in GitHub Desktop.
Android Post Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tracker.mainecivichackers.tracker; | |
import android.os.AsyncTask; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.CookieHandler; | |
import java.net.CookieManager; | |
import java.net.CookiePolicy; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLEncoder; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.TimeoutException; | |
/** | |
* Created by root on 6/15/15. | |
*/ | |
public class Connection { | |
public static final int UNAUTHORIZED = 401; | |
public static final int ACCEPTED = 202; | |
public static final int INTERNALERROR = 500; | |
public static final int INTERRUPTED = 0; | |
private int AsyncResult; | |
//Quick Hack | |
private String LoginString = null; | |
private boolean LoggedIn = false; | |
private HttpURLConnection _Connection; | |
String ServerUrl = "http://192.168.1.29/Tracker/index.php"; | |
public int DoLogin(String Username, String Password ) { | |
String TemploginString = "username=" + Username + "&password=" + Password; | |
try { | |
new SendPost().execute(TemploginString).get(10000, TimeUnit.MILLISECONDS); | |
} catch (Exception e) { | |
return INTERRUPTED; | |
} | |
if (AsyncResult == ACCEPTED) { | |
LoggedIn = true; | |
LoginString = TemploginString; | |
SavedConnection.Credentials = TemploginString; | |
} | |
return AsyncResult; | |
} | |
public void UpdateLocation(double Latitude, double Longitude) | |
{ | |
String PostData = SavedConnection.Credentials+"&Latitude="+Double.toString(Latitude)+"&Longitude="+Double.toString(Longitude); | |
System.out.println(PostData); | |
try { | |
new SendPost().execute(PostData).get(5000, TimeUnit.MILLISECONDS); | |
} catch (TimeoutException TOerror) { | |
//TimedOut | |
} catch (Exception genericError) | |
{ | |
} | |
if(AsyncResult == ACCEPTED) | |
{ | |
//Location Updated | |
} | |
} | |
public void Dispose() | |
{ | |
_Connection.disconnect(); | |
} | |
private class SendPost extends AsyncTask<String, Void, Void> { | |
@Override | |
protected Void doInBackground(String Data[]) { | |
HttpURLConnection connection = null; | |
try { | |
String request = ServerUrl; | |
URL _url = new URL(request); | |
Map<String, Object> params = new LinkedHashMap<>(); | |
StringBuilder postData = new StringBuilder(); | |
byte[] postBytes = Data[0].toString().getBytes("UTF-8"); | |
if(_Connection == null) | |
_Connection = (HttpURLConnection) _url.openConnection(); | |
_Connection.setRequestMethod("POST"); | |
_Connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | |
_Connection.setRequestProperty("Content-Length", String.valueOf(postBytes.length)); | |
_Connection.setDoOutput(true); | |
_Connection.getOutputStream().write(postBytes); | |
int Status = _Connection.getResponseCode(); | |
System.out.println("Pure Status: "+Status); | |
switch(Status) | |
{ | |
case(401): AsyncResult = UNAUTHORIZED; | |
break; | |
case(202): AsyncResult = ACCEPTED; | |
break; | |
case(500): AsyncResult = INTERNALERROR; | |
break; | |
default: //Not implemented | |
break; | |
} | |
} catch (MalformedURLException e) { | |
System.out.println("Error MalformedURL: "); | |
e.printStackTrace(); | |
} catch (IOException e) { | |
System.out.println("Error IO: "); | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment