简体   繁体   中英

Error in uploading file in Android by POST

I have written a Android program to upload a file to server by 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. 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
  2. Shut down Eclipse
  3. Created the AVD via the command line (eg 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)

ps Make sure you didn't delete the internet permission in your manifest file by accident. Also, while you are it, check and make sure the address work on your android browser.

Try flush DNS. (in windows: ipconfig /flushdns) Or change DNS provider.

Maybe there is a 2 '/' simbols in url ? "...server//api..." this must be like this "...server/api..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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