HttpUrlConnection仅在调试模式下有效

时间:2014-04-16 08:51:16

标签: android httpurlconnection

我通过AsyncTask在我的Android应用程序中调用http web服务。代码如下。它非常奇怪,只有连接我的USB调试电缆并且我从我的android工作室启动应用程序作为调试模式时,它才会起作用。如果我在设备上安装它而不进行调试,它就不会发送http请求。可能是什么问题呢。代码片段是:

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;
   }

1 个答案:

答案 0 :(得分:1)

我认为问题出在这一行:

android.os.Debug.waitForDebugger();

如果你在没有调试器的情况下启动......我会一直等待调试器。

希望有所帮助

相关问题