【发布时间】:2011-02-20 08:49:29
【问题描述】:
我正在尝试将几个值从 android 应用程序发送到我设置的 Web 服务。我正在使用 Http Post 发送它们,但是当我运行应用程序时,我收到错误请求时间失败 java.net.SocketException: Address family not supported by protocol。
我在使用模拟器和通过 wifi 连接的设备进行调试时得到了这个。我已经添加了互联网权限:
<uses-permission android:name="android.permission.INTERNET" />
这是我用来发送值的代码
void insertData(String name, String number) throws Exception {
String url = "http://192.168.0.12:8000/testapp/default/call/run/insertdbdata/";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
try {
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("a", name));
params.add(new BasicNameValuePair("b", number));
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);
}catch(Exception e){
e.printStackTrace();
}
我也知道我的网络服务工作正常,因为当我从 html 页面发送值时它工作正常 -
<form name="form1" action="http://192.168.0.12:8000/testapp/default/call/run/insertdbdata/" method="post">
<input type="text" name="a"/>
<input type="text" name="b"/>
<input type="submit"/>
我见过类似问题的问题,但还没有真正找到解决方案。
谢谢
【问题讨论】:
-
您的问题有点被切断(确保将您的 xml 代码包装在源代码块中)。