Introducir código aquí...public class Post {
private InputStream is = null;
private String respuesta = "";
@SuppressWarnings({ "unchecked", "rawtypes" })
private void conectaPost(ArrayList parametros, String URL) {
ArrayList nameValuePairs;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
byte[] data = ("user" + "pass").getBytes("UTF-8");
String nombreyclave = Base64.encodeToString(data, Base64.DEFAULT);
httpGet.addHeader("content-type", "mijson/json");
httpGet.addHeader("Authorization", nombreyclave);
httpGet.addHeader("User-Agent", "agente");
httpGet.addHeader("Cookie", "MiCookie");
nameValuePairs = new ArrayList<String>();
if (parametros != null) {
for (int i = 0; i < parametros.size() - 1; i += 2) {
nameValuePairs.add(new BasicNameValuePair(
(String) parametros.get(i), (String) parametros
.get(i + 1)));
}
// httpGet.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Buscar cabeceras en api
//httppost.setHeaders(headers)
}
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
} finally {
}
}
public String getRespuesta (){
return respuesta;
}
public void getRespuestaPost() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
//Log.e("log_tag", "Cadena JSon " + sb );
}
is.close();
respuesta = sb.toString();
} catch (Exception e) {
//Log.e("log_tag", "Error converting result " + e.toString());
}
}
@SuppressWarnings("finally")
public JSONArray getJsonArray() {
JSONArray jArray = null;
try {
jArray = new JSONArray(respuesta);
} catch (Exception e) {
} finally {
return jArray;
}
}
@SuppressWarnings("rawtypes")
public JSONArray getServerData(ArrayList parametros, String URL) {
try {
conectaPost(parametros, URL);
} catch (Exception e) {
// TODO: handle exception
}
if (is != null) {
getRespuestaPost();
}
if (respuesta != null && respuesta.trim() != "") {
return getJsonArray();
} else {
return null;
}
}