UnknownHost 尝试 POST - Android
任何人都知道为什么这可能会失败?该 URL 可以从我的浏览器访问,但当我尝试从我的应用程序内进行 POST 时,出现主机未找到的异常。
public void postData() {
//http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
File f = new File(filename);
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://dragonox.cs.ucsb.edu/Mosaic3D/clientupload.php";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("project", "globetrotter-test-00"));
nameValuePairs.add(new BasicNameValuePair("name", "globetrotter-test-00.jpg"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
FileBody bin = new FileBody(f);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", bin);
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}
Anyone have any ideas why this might be failing? The URL is accessible from my browser, but I get a host-not-found exception when I try to POST from within my app.
public void postData() {
//http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
File f = new File(filename);
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://dragonox.cs.ucsb.edu/Mosaic3D/clientupload.php";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("project", "globetrotter-test-00"));
nameValuePairs.add(new BasicNameValuePair("name", "globetrotter-test-00.jpg"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
FileBody bin = new FileBody(f);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", bin);
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
在
AndroidManifest.xml
文件try setting
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
in your
AndroidManifest.xml
file