I am calling http web service in my android app via AsyncTask. The code is as follow. It's super weird that it will only work when my usb debugging cable is connected and i'm launching the app as debug mode from my android studio. If i install it on device without debugging it, it won't send http request. what could be the problem. snippet of the code is:

public class ServiceHelper extends AsyncTask {
    private String txt;
    private Context context;
    private double latitude,longitude;
    public ServiceHelper(double lati, double     longi){
        latitude = lati;
        longitude = longi;
        this.context=context;
    }



    protected Object doInBackground(Object[] objects) {
        android.os.Debug.waitForDebugger();
        URL  url;
        try {
        url = new URL("http://somethinghere.com");
        HttpURLConnection conn;
        conn = (HttpURLConnection) url.openConnection();

        String param="lat=" + URLEncoder.encode(String.valueOf(latitude),"UTF-8")+
                        "&long=" + URLEncoder.encode(String.valueOf(longitude),"UTF-8");
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setFixedLengthStreamingMode(param.getBytes().length);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(param);
        out.close();
        String response= "";
        Scanner inStream = new Scanner(conn.getInputStream());

        //process the stream and store it in StringBuilder


    } catch (IOException e) {
        e.printStackTrace();
    }
    catch (Exception e){
        e.printStackTrace();
    }
    return null;
   }
有帮助吗?

解决方案

Well i suppose the problem is in this line:

android.os.Debug.waitForDebugger();

If you start without debugger... it keeps waiting for the debugger i think.

Hope it helps

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top