2012-10-07 15 views
0

我已经编写了一个Android程序,通过HTTP POST将文件上传到服务器。通过POST在Android中上传文件的错误

此前它工作正常,但我不知道为什么它现在不工作。 我正在使用我的Android设备进行测试。 我刚刚检查过它使用模拟器工作正常。

当我在浏览器中打开该链接时,它仍然正常工作正常并打开。

做任何机构可以告诉我可能是什么问题?

我得到这个错误:(没有与主机名关联的地址)

10-07 04:28:14.410: I/System.out(1280): executing request POST http:////path/to/my/server//api/index.php/match HTTP/1.1 
10-07 04:28:14.450: W/System.err(1280): java.net.UnknownHostException: Unable to resolve host "//path/to/my/server/": No address associated with hostname 

这里是我的代码...

private class UploadFilesTask extends AsyncTask<File, Void, Void> { 
     @Override 
     protected Void doInBackground(File... arg0) { 

      HttpClient httpclient = new DefaultHttpClient(); 
       httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
enter code here 
        // I have not shown my REAL server address due so some restriction, So assume below URL is correct 

       HttpPost httppost = new HttpPost("http://path/to/my/server/"); //Assume path is correct 
       //File file = new File("/mnt/sdcard/DCIM/Camera/01.jpg"); 

       MultipartEntity mpEntity = new MultipartEntity(); 
       ContentBody cbFile = new FileBody(arg0[0], "image/jpeg"); 
       mpEntity.addPart("userfile", cbFile); 


       httppost.setEntity(mpEntity); 
       System.out.println("executing request " + httppost.getRequestLine()); 
       HttpResponse response = null; 
       try { 
        response = httpclient.execute(httppost); 
       } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 

        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 

        e.printStackTrace(); 
       } 
       HttpEntity resEntity = response.getEntity(); 

       System.out.println(response.getStatusLine()); 
       if (resEntity != null) { 
        try { 
         //audioFilename = EntityUtils.toString(resEntity); 
         System.out.println(EntityUtils.toString(resEntity)); 
       } catch (ParseException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       } 
       if (resEntity != null) { 
        try { 
        resEntity.consumeContent(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       } 

       httpclient.getConnectionManager().shutdown(); 
       return null; 
     } 
    } 
+0

感谢所有的答复。 完成。 – Vishal

+0

原因是什么?如果你能分享它,会很好,所以如果他们遇到同样的问题,其他人也可以解决它。 – Infinity

回答

0

请尝试以下

  1. 删除您的AVD
  2. 关闭Eclipse
  3. 通过命令行创建AVD(例如, android创建avd -n my_android1.5 -t 2)
  4. 使用选项-dns-server 8.8.8.8(例如emulator -avd my_android1.5 -dns-server 8.8.8.8)从命令行启动它

ps确保您没有意外删除清单文件中的Internet权限。此外,虽然你是这样,检查并确保您的Android浏览器的地址工作。

+0

嗨 我在我的清单中有Internet权限。 我正在测试我的Android设备。 让我通过重新启动一切来检查。 – Vishal

+0

首先,尝试在模拟器上运行它。我们要仔细检查它的设备或代码的错误。 – Infinity

+0

嗨 在模拟器上工作正常。 那么,我需要更改我的设备中的任何权限? 我有在下面 许可<使用的许可机器人:名称=“android.permission.INTERNET对” /> <使用的许可机器人:名称=“android.permission.READ_EXTERNAL_STORAGE” /> <使用的许可机器人:name =“android.permission.WRITE_EXTERNAL_STORAGE”/>在我的清单中 – Vishal

0

尝试刷新DNS。 (在windows:ipconfig/flushdns) 或更改DNS提供程序。

0

也许在url中有2个'/'符号? “...服务器// api ...”这必须是这样的“...服务器/ api ...”