2013-05-08 55 views
-6

在下面的代码中,当运行我的android项目从第二个布局发送json到cloudant.com时出现错误,因此我得到了一个错误。 .os.NetworkOnMainThreadException desde EL logcat的android.os.NetworkOnMainThreadException json android

//Obtenemos una referencia al LocationManager 
    locManager = 
     (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    //Obtenemos la última posición conocida 
    final Location loc = 
     locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 



    // buton ejecuta 
    btnActualizar.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     // comenzarLocalizacion(); 



      HttpPost request = new HttpPost("https://tonatiuhnava.cloudant.com/android/"); 
      request.addHeader("Content-Type", "application/json"); 
      request.addHeader("Authorization", "Basic " + Base64.encodeToString("tonatiuhnava:70n471uh".getBytes(), Base64.NO_WRAP)); 
      JSONObject cadena = new JSONObject(); 

      if(loc != null) 
      { 

      try{ 
       cadena.put("nombre", "tonatiuhNava");//Le asignamos los datos que necesitemos 
       cadena.put("Category", new Integer(01)); 
       cadena.put("Event", new Integer(201)); 

       cadena.put("Fecha",curTime); 
       //cadena.put("Fecha", currentDate.toGMTString()); 
       cadena.put("longitud", new Double(loc.getLongitude())); 
       cadena.put("Latitud", new Double(loc.getLatitude())); 

      } catch (JSONException e) 
      { 
       Log.d("WAZAAA" , "error json"); 

      } 

      try{ 
       request.setEntity(new StringEntity( cadena.toString())); 

      } catch (Exception e){ 
       Log.d("WAZAAA" , "error entity"); 

      } 

      DefaultHttpClient connection = new DefaultHttpClient(); 
      try{ 
      Log.d("WAZAAA" , "haciendo request"); 

      HttpResponse execute = connection.execute(request); 
      Log.d("WAZAAA" , "request hecho"); 

      byte[] bytes = new byte[1000]; 
      int numRead = 0; 
      StringBuilder x = new StringBuilder(); 
      InputStream stream = execute.getEntity().getContent(); 
      stream.read(bytes); 
      x.append(new String(bytes, 0, numRead)); 



      Log.d("WAZAAA" , execute.getStatusLine().getReasonPhrase()); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       Log.e("WAZAAA" , "client error"); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Log.e("Internet Please :..(" , "IO error"); 
      } 
      Log.d("WAZAAA" , "fin"); 

      // termina if 
      }else { 
       Context context = getApplicationContext(); 
       CharSequence text = "Espere a que cargue coordenadas..."; 
       int duration = Toast.LENGTH_SHORT; 

       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 


      } 

     } 

回答

2

阻止请求不能在UI线程上运行,他们应该在一个单独的线程上运行。使用AsyncTask。阅读this

+0

AsyncTask?一个例子? – tonatiuhNava 2013-05-08 14:46:38

+0

阅读指南。 – Blackbelt 2013-05-08 14:47:28

相关问题