UnknownHost尝试POST - Android

时间:2011-03-01 04:17:52

标签: android networking http-post

任何人都有任何想法为什么会失败?可以从我的浏览器访问该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

1 个答案:

答案 0 :(得分:1)

尝试设置

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

在您的AndroidManifest.xml文件中