UnknownHost 尝试 POST - Android

发布于 2024-10-19 16:26:38 字数 1459 浏览 3 评论 0原文

任何人都知道为什么这可能会失败?该 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();
    }


}

UnknownHostException

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();
    }


}

UnknownHostException

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随波逐流 2024-10-26 16:26:38

尝试

AndroidManifest.xml 文件

try setting

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

in your AndroidManifest.xml file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文