安卓上传字符串到php服务器,php – Android:将文件与其他POST字符串一起上传到页面...

我正在尝试将图像上传到

PHP页面以及有关图像的其他一些信息,以便PHP页面知道如何处理它.目前,我正在使用它:

URL url = new URL("http://www.tagverse.us/upload.php?authcode="+WEB_ACCESS_CODE+"&description="+description+"&userid="+userId);

HttpURLConnection connection = (HttpURLConnection)url.openConnection();

connection.setDoOutput(true);

connection.setRequestMethod("POST");

OutputStream os = connection.getOutputStream();

InputStream is = mContext.getContentResolver().openInputStream(uri);

BufferedInputStream bis = new BufferedInputStream(is);

int totalBytes = bis.available();

for(int i = 0; i < totalBytes; i++) {

os.write(bis.read());

}

os.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String serverResponse = "";

String response = "";

while((response = reader.readLine()) != null) {

serverResponse = serverResponse + response;

}

reader.close();

bis.close();

除了拥有GET / POST混合之外,还有更优雅的解决方案吗?我觉得好像这很草率,但据我所知,这是一个完全可以接受的解决方案.如果有更好的方法,我会感谢被指向正确的方向.谢谢!

PS:我很熟悉在正常情况下你会如何通过POST与PHP页面进行交互:

HttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost("http://www.tagverse.us/login.php");

try {

List pairs = new ArrayList();

pairs.add(new BasicNameValuePair("authcode", WEB_ACCESS_CODE));

pairs.add(new BasicNameValuePair("username", username));

pairs.add(new BasicNameValuePair("password", password));

post.setEntity(new UrlEncodedFormEntity(pairs));

client.execute(post);

}

基本上我想要做的是组合这两种方法,但因为我使用的是HttpURLConnection对象而不是HttpPost对象,所以它并不像仅仅合并这两种方法那么简单.

谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值