You are on page 1of 3

1.- insertar la sig. linea en granel.

app

implementation
‘org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.a
pache.http.client:4.1.2'

2.- crear un activity basic


insertar un edittect
insertar un botón
insertar un listview

3.- crear un metodo para el botón

public void btnPost(View view) {

new leeJSONPOST().execute(
"http://sistematizaciongro.com.mx/push/
webservice1.php" );

4.- insertar la clase asyntask


private class leeJSONPOST extends AsyncTask<String, String,
String> {

protected String doInBackground(String... urls) {


return readJSON_Post(urls[0]);
}

protected void onPostExecute(String result) {

try {
JSONObject jsonObject = new JSONObject(result);
JSONArray OJson =
jsonObject.getJSONArray("items");
String campo1="";
//recorremos el arreglo
for (int i = 0; i < OJson.length(); i++) {
JSONObject c = OJson.getJSONObject(i);
String tit = c.getString("titulo");
txtresp.setText(tit);
campo1 = campo1 + tit + "\n";

txtresp.setText(campo1);

} catch (Exception e) {
Log.d("JSON", "error en datosssss");
txtresp.setText("error en datos..post..");
}
}

public String readJSON_Post(String URL) {


StringBuilder stringBuilder = new StringBuilder();
try {

HttpClient httpclient = new DefaultHttpClient();

/*Creamos el objeto de HttpClient que nos


permitira conectarnos mediante peticiones http*/

HttpPost httppost = new HttpPost(URL);

/*El objeto HttpPost permite que enviemos una


peticion de tipo POST a una URL especificada*/

//AÑADIR PARAMETROS

List<NameValuePair> params = new ArrayList<>();

params.add(new BasicNameValuePair("us",
txtP1.getText().toString()));

// params.add(new BasicNameValuePair("p1",
txtP2.getText().toString()));

/*Una vez añadidos los parametros actualizamos la


entidad de httppost, esto quiere decir en pocas palabras anexamos
los parametros al objeto para que al enviarse al servidor envien
los datos que hemos añadido*/

httppost.setEntity(new
UrlEncodedFormEntity(params));

/*Finalmente ejecutamos enviando la info al


server*/

HttpResponse resp = httpclient.execute(httppost);

// HttpEntity ent = resp.getEntity();/*y


obtenemos una respuesta*/

// String text = EntityUtils.toString(ent);

// return text;

StatusLine statusLine = resp.getStatusLine();


int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = resp.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
Log.d("JSON:---->",
stringBuilder.toString());
// txtres.setText(stringBuilder.toString());

} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();

}
}

You might also like