Android的文件上传不工作 [英] Android fileupload not working
本文介绍了Android的文件上传不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我与Android合作。我已经创建了一个Android应用程序这将文件上载到服务器。我用下面的code
<一个href=\"http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106\" rel=\"nofollow\">http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106
但我不能上传我的文件。这个应用程序显示上传开始,但没有文件被上传到我的服务器位置。
解决方案
私有类SendHtt prequestTask延伸的AsyncTask&LT;弦乐,太虚,字符串&GT; { 保护字符串doInBackground(字符串... PARAMS){ 字符串URL =参数[0]; 串的param1 =参数[1]; 字符串参数2 =参数[2]; 位图B = BitmapFactory.de codeResource(UploadActivity.this.getResources(),R.drawable.logo); ByteArrayOutputStream BAOS =新ByteArrayOutputStream(); b.com preSS(比较pressFormat.PNG,0,BAOS); 尝试{ HttpClient的客户端=新的HttpClient(URL); client.connectForMultipart(); client.addFormPart(参数1参数1); client.addFormPart(参数2,参数2); client.addFilePart(文件,为Logo.png,baos.toByteArray()); client.finishMultipart(); 字符串数据= client.getResponse(); } 捕获(的Throwable t)的{ t.printStackTrace(); } 返回null; } @覆盖 保护无效onPostExecute(字符串数据){ item.setActionView(NULL); } }
Hi I am working with android. I have created an android app which uploads a file to the server. I have used the following code
But I am not able to upload my file. The App shows uploading started but no file gets uploaded to my server location.
解决方案
private class SendHttpRequestTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
String url = params[0];
String param1 = params[1];
String param2 = params[2];
Bitmap b = BitmapFactory.decodeResource(UploadActivity.this.getResources(), R.drawable.logo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
b.compress(CompressFormat.PNG, 0, baos);
try {
HttpClient client = new HttpClient(url);
client.connectForMultipart();
client.addFormPart("param1", param1);
client.addFormPart("param2", param2);
client.addFilePart("file", "logo.png", baos.toByteArray());
client.finishMultipart();
String data = client.getResponse();
}
catch(Throwable t) {
t.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String data) {
item.setActionView(null);
}
}
这篇关于Android的文件上传不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文