我已经编写了一个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;
}
}
感谢所有的答复。 完成。 – Vishal
原因是什么?如果你能分享它,会很好,所以如果他们遇到同样的问题,其他人也可以解决它。 – Infinity