简体   繁体   English

通过POST在Android中上传文件时出错

[英]Error in uploading file in Android by POST

I have written a Android program to upload a file to server by HTTP POST. 我已经编写了一个Android程序,可以通过HTTP POST将文件上传到服务器。

Earlier its was working fine but I don't know why it is not working now. 以前它可以正常工作,但我不知道为什么它现在不工作。 I am testing this with my Android Device. 我正在使用Android设备对此进行测试。 I Have just checked that It is working fine with emulator. 我刚刚检查了它是否可以在模拟器上正常工作。

When I open that link in browser, Then it is still working fine and open correctly. 当我在浏览器中打开该链接时,它仍然可以正常工作并正确打开。

Do any body can tell me what could be the problem??? 有谁能告诉我可能是什么问题???

I am getting this error: (No Address associated with hostname) 我收到此错误:(没有与主机名关联的地址)

 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

Here is my code... 这是我的代码...

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

Try the following 尝试以下

  1. Delete your AVD 删除您的AVD
  2. Shut down Eclipse 关闭Eclipse
  3. Created the AVD via the command line (eg android create avd -n my_android1.5 -t 2) 通过命令行创建了AVD(例如android create avd -n my_android1.5 -t 2)
  4. Launched it from the command line with the option -dns-server 8.8.8.8 (eg emulator -avd my_android1.5 -dns-server 8.8.8.8) 使用选项-dns-server 8.8.8.8从命令行启动它(例如,模拟器-avd my_android1.5 -dns-server 8.8.8.8)

ps Make sure you didn't delete the internet permission in your manifest file by accident. ps确保您没有意外删除清单文件中的Internet权限。 Also, while you are it, check and make sure the address work on your android browser. 另外,在使用时,请检查并确保该地址在您的android浏览器上正常工作。

Try flush DNS. 尝试刷新DNS。 (in windows: ipconfig /flushdns) Or change DNS provider. (在Windows中:ipconfig / flushdns)或更改DNS提供程序。

Maybe there is a 2 '/' simbols in url ? 网址中是否可能有2'/'simbols? "...server//api..." this must be like this "...server/api..." “ ...服务器// api ...”必须类似于此“ ...服务器// api ...”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM