我正试图从android发布到GAE,我得到了未知的主机异常。这是我的代码,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://silver-kites.appspot.com");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("abc", "123"));
nameValuePairs.add(new BasicNameValuePair("xyz", "456"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream inputStream = entity.getContent();
ResponseText.setText(convertStreamToString(inputStream));
}
} catch (ClientProtocolException e) {
ResponseText.setText(e.getMessage());
} catch (IOException e) {
ResponseText.setText(e.getMessage());
}
当我尝试连接到localhost时:8888 am连接被拒绝。我不知道如何通过这个。帮助我。
答案 0 :(得分:2)
我也试过了,在android中仍然遇到Unknown Host异常。我是否需要进行任何访问HTTP的设置?或者在模拟器中http不会工作?
我在android manifest xml中设置了<uses-permission android:name="android.permission.INTERNET"></uses-permission>
。仍然没有希望。
有什么帮助吗?我检查了GAE,我的请求没有到达那里。
这是我的代码,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://silver-kites.appspot.com/silverscores");
try {
nameValuePairs.add(new BasicNameValuePair("name", "JP"));
nameValuePairs.add(new BasicNameValuePair("seconds", "123"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream inputStream = entity.getContent();
ResponseText.setText(convertStreamToString(inputStream));
}
} catch (ClientProtocolException e) {
ResponseText.setText(e.getMessage());
} catch (IOException e) {
ResponseText.setText(e.getMessage());
}
答案 1 :(得分:1)
我热议了同样的问题。
经过一些谷歌搜索和stackoverflowing :)我找到了解决方案:
检查您的模拟器是否有网络 连接(网络图标) 通知栏) - 我的模拟器 没有,因为防火墙被阻止了 它:(。我发现了这个提示 here
检查您是否设置了网络权限 为您的应用添加行
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
AndroidManifest.xml
中的文件
根据Emulator Networking,应使用10.0.2.2
代替localhost
/ 127.0.0.1
。
localhost
/ 127.0.0.1
是Android设备的IP地址,而非开发者计算机。
答案 2 :(得分:0)
尝试使用以下(名称,值)对
nameValuePairs.add(new BasicNameValuePair("name", "123"));
nameValuePairs.add(new BasicNameValuePair("seconds", "456"));
现在您不会收到无效参数异常,但此POST的响应为“Sever Error”,您可以向GAE查询此错误。
答案 3 :(得分:0)
我做得很好。只有一个问题是模拟器在飞行模式下运行,所以它没有使用互联网连接。所以我得到了未知的主机异常。现在工作正常。感谢您的支持。