my code stock.com

Snippet options

Download: Download snippet as insert-json-array.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

package tasks;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import objetos.Denuncia;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.coffeeandcookies.sube.app;

public class insertValoracionTask
{
	public static String insertPunto(String token, 
			int idPunto,
			int tipoValoracion, ArrayList<Denuncia>denuncias) throws JSONException
	{
		String URL = app.url_insert_valoracion;
		HttpPost httpPost = null;
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		httpPost = new HttpPost(URL);
		nameValuePairs.add(new BasicNameValuePair("idUsuario", "1"));
		nameValuePairs.add(new BasicNameValuePair("id_punto", String.valueOf(idPunto)));
		nameValuePairs.add(new BasicNameValuePair("tipo_valoracion", String.valueOf(tipoValoracion)));
		
		JSONArray jsonArray = new JSONArray();
		for (Denuncia d : denuncias)
		{
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", d.getId());
			jsonObject.put("valor", d.getValor());
			jsonObject.put("input", d.getInput());
			jsonArray.put(jsonObject);
		}
		
		nameValuePairs.add(new BasicNameValuePair("arr", String.valueOf(jsonArray.toString())));
		Log.d(app.TAG,"URL: "+URL+" json: "+jsonArray.toString());
		
		try
		{
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
		}
		catch (UnsupportedEncodingException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();

		httpPost.setHeader(app.TOKEN_HEADER, token);
		httpPost.setHeader("Accept", "application/json");
		httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

		try
		{
			HttpResponse response = client.execute(httpPost);
			HttpEntity entity = response.getEntity();
			InputStream content = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(content));
			String line;
			while ((line = reader.readLine()) != null)
			{
				builder.append(line);
			}
			if (builder.toString().length() > 0)
			{
				return "";
			}
		}
		catch (Exception e)
		{
			Log.e(app.TAG, "Error insert valoracion");
			e.printStackTrace();
		}
		return "Error";
	}
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.