gpt4 book ai didi

Android MultipartEntity POST 接收除 FileBody 之外的所有内容

转载 作者:太空宇宙 更新时间:2023-11-03 11:17:54 25 4
gpt4 key购买 nike

阅读大量问题和答案后,我决定使用 MultipartEntity。这个答案必须是最好的: https://stackoverflow.com/a/2937140/1388723

我已经实现了:

private class SendImage extends AsyncTask<String, Void, Void> {
String sresponse;

@Override
protected Void doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(
"http://taras.xin.com.pl/loadimage.php");
InputStream in = null;
try {
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
Log.i("MultyPart", params[0]);
File file = new File(params[0]);
Log.i("MultyPartFILE", file.toString());
ContentBody fb = new FileBody(file, "image/jpeg");
Log.i("MultyPartFB", fb.toString());
entity.addPart("text", new StringBody(
"This text was added to posrt"));
entity.addPart("word", new StringBody(
"This text was added to post"));
entity.addPart("file", fb);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
in = response.getEntity().getContent();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(in, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
in.close();
sresponse = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

return null;
}

@Override
protected void onPostExecute(Void result) {

runOnUiThread(new Runnable() {

@Override
public void run() {
text.setText(sresponse);
}
});

super.onPostExecute(result);
}
}

和我的 loadimage.php:

    <?php
var_dump($_POST);
var_dump($_FILE);
?>

我接收到 $_POST 的两个字符串,但没有 FileBody(file) 的影子。$_FILE 给出空值。

确切地说,我已经下载了 httpcomponents-client-4.2.2-bin,提取了 httpmime-4.2.2.jar。

最佳答案

尝试只获取你发送的文件
$file = $_FILES['文件'];

关于Android MultipartEntity POST 接收除 FileBody 之外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13801926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com