[android][Resuelto]Error al Hacer Post

Iniciado por LaThortilla (Effort), 2 Mayo 2015, 18:49 PM

0 Miembros y 1 Visitante están viendo este tema.

LaThortilla (Effort)

Buen Dia..

Alguien me podría explicar por que es el error en la linia 38?

Código (java) [Seleccionar]
package.test.wservice;

import android.util.Log;

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.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;


/**
* Created by efort on 4/30/2015.
*/
public class WServiceHelper {
   private static final String URL = "http://192.168.1.163/ws/index.php";


   public WServiceHelper(){
   }

   public String post(String posturl){

       try {

           HttpClient httpclient = new DefaultHttpClient();

           HttpPost httppost = new HttpPost(posturl);


           HttpResponse resp = httpclient.execute(httppost);

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

           String text = EntityUtils.toString(ent);

           return text;

       }

       catch(Exception e) { return "error";}

   }



   public void getInfoCelular(String celular) {
       String str = this.post(URL);
       Log.i(PhoneListener.LOG_TAG,str);
   }




}



Código (ada) [Seleccionar]
05-02 11:41:07.057  13473-13473/test.wservice W/System.err﹕ android.os.NetworkOnMainThreadException
05-02 11:41:07.082  13473-13473/test.wservice W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
05-02 11:41:07.082  13473-13473/test.wservice W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-02 11:41:07.082  13473-13473/test.wservice W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:159)
05-02 11:41:07.082  13473-13473/test.wservice W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
05-02 11:41:07.082  13473-13473/test.wservice W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-02 11:41:07.083  13473-13473/test.wservice W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-02 11:41:07.083  13473-13473/test.wservice W/System.err﹕ at java.net.Socket.connect(Socket.java:859)
05-02 11:41:07.083  13473-13473/test.wservice W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-02 11:41:07.083  13473-13473/test.wservice W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-02 11:41:07.083  13473-13473/test.wservice W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-02 11:41:07.083  13473-13473/test.wservice W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-02 11:41:07.083  13473-13473/test.wservice W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
05-02 11:41:07.083  13473-13473/test.wservice W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-02 11:41:07.084  13473-13473/test.wservice W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-02 11:41:07.084  13473-13473/test.wservice W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-02 11:41:07.084  13473-13473/test.wservice W/System.err﹕ at test.wservice.WServiceHelper.post(WServiceHelper.java:55)
05-02 11:41:07.084  13473-13473/test.wservice W/System.err﹕ at test.wservice.wservice.WServiceHelper.getInfoCelular(WServiceHelper.java:75)
05-02 11:41:07.084  13473-13473/test.wservice W/System.err﹕ at test.wservice.servicio.PhoneListener.onCallStateChanged(PhoneListener.java:28)
05-02 11:41:07.084  13473-13473/test.wserviceW/System.err﹕ at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:370)
05-02 11:41:07.084  13473-13473/test.wservice W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:107)
05-02 11:41:07.085  13473-13473/test.wservice W/System.err﹕ at android.os.Looper.loop(Looper.java:194)
05-02 11:41:07.085  13473-13473/test.wservice W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5392)
05-02 11:41:07.085  13473-13473/test.wservice W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
05-02 11:41:07.085  13473-13473/test.wservice W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
05-02 11:41:07.085  13473-13473/test.wservice W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-02 11:41:07.085  13473-13473/test.wservice W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-02 11:41:07.086  13473-13473/test.wservice/System.err﹕ at dalvik.system.NativeStart.main(Native Method)


SOLUCION:

Código (java) [Seleccionar]
Thread thread = new Thread(new Runnable(){
   @Override
   public void run() {
      //El try  catch TIENE QUE IR AQUÍ
   }
});

thread.start();