【问题标题】:Android httppost network violationAndroid httppost 网络违规
【发布时间】:2011-12-24 20:31:18
【问题描述】:

我的 Android 应用程序有问题。我的意思是我正在使用来自互联网上的一个 tuts 的代码,如下所示

public static String sendLocation(String s) {
    // Create a new HttpClient and Post Header
    //HttpParams params = new BasicHttpParams();
    //params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://google.com/read.php");
    //String d = "ok";

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("user", "mobile"));
        nameValuePairs.add(new BasicNameValuePair("location", s));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
      //  d = "CPE";
    } catch (IOException e) {
       // d = "E";
    }
    return "send";
} 

}`

当应用程序尝试向远程服务器发送数据时出现问题/错误(下一行)

// Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost);

我也使用 StrictMode,之后一切正常,日志显示网络违规

11-08 22:34:40.020:D/StrictMode(939):StrictMode 策略违规; ~duration=359 毫秒:android.os.StrictMode$StrictModeNetworkViolation:策略=31 违规=4

我在清单文件中包含了适当的权限

<uses-permission 
    android:name="android.permission.ACCESS_FINE_LOCATION" />

您知道在没有 StrictMode 的情况下我应该怎么做才能运行我的应用程序(实际上是通过 post 将数据发送到服务器)?

【问题讨论】:

    标签: android http-post


    【解决方案1】:

    问题是您试图在主 UI 线程上进行网络调用。仅在单独的线程中使用这些类型的方法。

    is here. 的一个很好的例子

    【讨论】:

    • “策略=31 违规=4”是什么意思?是否有包含政策和违规列表的表格? 31 和 4 是什么意思?,谢谢
    • 请参阅this question,了解如何了解政策和违规情况。
    猜你喜欢
    • 2014-11-01
    • 2012-03-30
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多