【发布时间】: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 将数据发送到服务器)?
【问题讨论】: