2016-08-19 42 views
0

这是我的Android从Android的发送数组到Web服务

public String addRelation(ArrayList<Medida> pMedidas, Equipo pEquipo) throws UnsupportedEncodingException { 

    String eTagEquipo = Seguridad.encriptar(String.valueOf(pEquipo.getTag())); 
    ArrayList<String> eMedidas = new ArrayList<>(); 
    int size = pMedidas.size(); //Obtenemos la cantidad de medidas relacionadas con el equipo 

    for(int x=0;x<pMedidas.size();x++) { 
     eMedidas.add(String.valueOf(pMedidas.get(x).getId())); 
    } 
    String urlParametros = "tagEquipo=" + eTagEquipo + 
      "&idMedidas[]=" + eMedidas; 
    HttpURLConnection conn = null; 
    StringBuffer sb = null; 
    try{ 
     URL url = new URL ("http://www.myUrl.com/myFolder/WebService/myFolder/relEquipoMedidas.php"); 
     conn = (HttpURLConnection)url.openConnection(); 
     conn.setRequestMethod("POST"); //Método POST 

     //Longitud de datos que se envian 

     conn.setRequestProperty("Content-Length", ""+ Integer.toString(urlParametros.getBytes().length)); 
     conn.setDoOutput(true); //Comando para la salida de datos 

     DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); 
     wr.writeBytes(urlParametros); 
     wr.close(); 

     int responseCode=conn.getResponseCode(); 
     if (responseCode == HttpsURLConnection.HTTP_OK) { 
      BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      sb = new StringBuffer(""); 
      String line=""; 
      while((line = in.readLine()) != null) { 
       sb.append(line); 
       break; 
      } 
      in.close(); 
      return sb.toString(); 
     } 
     else { 
      return new String("False"); 
     } 
    } 
    catch(Exception e){ 
     return new String("Exception"); 
    } 

} 

代码,这是我在Web服务代码(PHP):

require_once '../../x/Conexion.php'; 

//----Recibimos los datos para realizar un alta----- 
$tagEquipo= filter_input(INPUT_POST, 'tagEquipo', FILTER_SANITIZE_STRING); //Seguridad de saneamiento 
$idMedidas= (array)$_POST['idMedidas']; //Seguridad de saneamiento 

//-------------------------------------------------- 

$db = new Conexion(); 
$array = array(); 
//Primero se consulta que no exista actualmente 
$cons = $db->query("SELECT _id FROM t__equipos WHERE _tag = '$tagEquipo';"); 
$idEquipo = $db->recorrer($cons)[0]; 
$cant = count($idMedidas); 
$i = 0; 
while($i < $cant) 
{ 
    $ins = $db->query("INSERT INTO t__EquiMed(_idEquipo, _idMedida, _ordenMedida) VALUES ('$idEquipo', '$idMedidas[$i]', ($i+1))"); 

    $i++; 
} 
$array[0]['_resultado'] = 1; 
echo json_encode($array); 
+0

具有u解决问题了吗? – Tony

+0

是我的朋友!我用“for”和我添加的每个参数。 – Strelok

回答

0

你需要转换你的数组列表到JSON字符串数组,你可以通过很多方式做到这一点(你可以看看GSONJackson) 然后ü需要做到这一点

 String data = jsonarray string; 
     OutputStream outputStream = urlConnection.getOutputStream(); 
     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
     writer.write(data); 
     writer.close(); 
     outputStream.close(); 

而且它会更好,如果你使用的网络库,而使用像volleyIon默认URL连接使用,Retrofit