Created
September 19, 2017 15:27
-
-
Save FuriosoJack/3328709e84c302ce45c15b89500e4c59 to your computer and use it in GitHub Desktop.
Clase para hacer peticion post y get desde java Android es necesario usar el patron de diseño observador
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
import android.app.ProgressDialog; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import com.pcflex.android.patrones.ObjObervador; | |
import com.pcflex.android.patrones.ObjObservable; | |
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLEncoder; | |
import java.util.Iterator; | |
import javax.net.ssl.HttpsURLConnection; | |
/** | |
* Created by Juan on 18/09/2017. | |
*/ | |
public class SendPostRequest extends AsyncTask<String, Void, String> implements ObjObservable { | |
private String url; | |
private JSONObject parametros; | |
private String respuesta; | |
private ObjObervador obervador; | |
private ProgressDialog pDialog; | |
public SendPostRequest(String url, JSONObject parametros, ProgressDialog dialog){ | |
this.url = url; | |
this.parametros = parametros; | |
this.pDialog = dialog; | |
} | |
public void setObervador(ObjObervador obervador) { | |
this.obervador = obervador; | |
} | |
protected void onPreExecute(){ | |
if(this.pDialog !=null) { | |
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); | |
pDialog.setCancelable(false); | |
pDialog.show(); | |
} | |
} | |
protected String doInBackground(String... arg0) { | |
try { | |
URL url = new URL(this.url); // here is your URL path | |
Log.e("Respuesta URL",this.url); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setReadTimeout(15000 /* milliseconds */); | |
conn.setConnectTimeout(15000 /* milliseconds */); | |
conn.setRequestMethod("POST"); | |
conn.setDoInput(true); | |
conn.setDoOutput(true); | |
OutputStream os = conn.getOutputStream(); | |
BufferedWriter writer = new BufferedWriter( | |
new OutputStreamWriter(os, "UTF-8")); | |
writer.write(getPostDataString()); | |
writer.flush(); | |
writer.close(); | |
os.close(); | |
int responseCode=conn.getResponseCode(); | |
if (responseCode == HttpsURLConnection.HTTP_OK) { | |
Log.e("resultado","Ok"); | |
BufferedReader in=new BufferedReader( | |
new InputStreamReader( | |
conn.getInputStream())); | |
StringBuffer sb = new StringBuffer(""); | |
String line=""; | |
while((line = in.readLine()) != null) { | |
sb.append(line); | |
break; | |
} | |
in.close(); | |
return sb.toString(); | |
} | |
else { | |
Log.e("resultado","no 200"); | |
return new String("201"); | |
} | |
} | |
catch(Exception e){ | |
Log.e("resultado","Fallo en la conexion"); | |
return new String("500"); | |
} | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
pDialog.dismiss(); | |
this.respuesta = result; | |
this.notificar(); | |
} | |
public String getPostDataString() throws Exception { | |
StringBuilder result = new StringBuilder(); | |
boolean first = true; | |
Iterator<String> itr = this.parametros.keys(); | |
while(itr.hasNext()){ | |
String key= itr.next(); | |
Object value = this.parametros.get(key); | |
if (first) | |
first = false; | |
else | |
result.append("&"); | |
result.append(URLEncoder.encode(key, "UTF-8")); | |
result.append("="); | |
result.append(URLEncoder.encode(value.toString(), "UTF-8")); | |
} | |
return result.toString(); | |
} | |
@Override | |
public void notificar() { | |
this.obervador.update(this.respuesta); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uso