我编写了一个Android程序,通过HTTP POST将文件上传到服务器。
早些时候它的工作正常,但我不知道为什么它现在不起作用。 我正在用我的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 :(得分:0)
尝试以下
P.S。确保您没有意外删除清单文件中的Internet权限。此外,当你是它,检查并确保地址在你的Android浏览器上工作。
答案 1 :(得分:0)
尝试刷新DNS。 (在windows中:ipconfig / flushdns) 或者更改DNS提供商。
答案 2 :(得分:0)
也许网址中有2个/'符号? “... server // api ...”这必须像这样“... server / api ...”