【发布时间】:2014-02-08 03:34:41
【问题描述】:
我正在使用 HttpClient 从我的 Android 设备向一个简单的 HTTP 服务器发出请求,该服务器包含一个用于检索图片的 PHP 脚本。图片在 blob 数据中可用。这意味着如果我在服务器端执行这个小 PHP 代码:
file_put_contents("myPicture.png", $blob_data);
我使用名为 myPicture.png 的文件将图片保存在服务器中。现在我想获取这个 $blob_data(我的图片)以将其保存在我的 Android 设备中,而不是 HTTP 服务器中。有人可以给我一个提示,让我从 PHP 脚本中返回 blob 数据并获取 Android 设备内的图片以将其存储在本地?
这是我的 HTTPClient:
@Click(R.id.btn_login)
@Background
public void LoginTrigger(){
String nUsername = username.getText().toString().trim();
String nPassword = password.getText().toString().trim();
if (nUsername.matches("") || nPassword.matches("")){
displayToast("Please insert your login!");
}
else{
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("username", nUsername));
urlParameters.add(new BasicNameValuePair("password", nPassword));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(SERVER_PROXY);
post.setHeader("User-Agent", SERVER_USER_AGENT);
try{
post.setEntity(new UrlEncodedFormEntity(urlParameters, "utf-8"));
}
catch(Exception e){
Log.getStackTraceString(e);
}
HttpResponse response = null;
try{
response = client.execute(post);
Log.e("Response Code : ", " = " + response.getStatusLine().getReasonPhrase());
InputStream inputStream = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
String responseMessage = sb.toString();
checkLogin(responseMessage);
}
catch(Exception e){
Log.e("HTTP-ERROR", " = " + Log.getStackTraceString(e));
displayToast("Check your Internet connection!");
}
}
}
当我在 PHP 脚本中回显它时,字符串 responseMessage 包含我的 blob 数据。我只需要将这个流放在位图或其他东西中,但我不知道如何完成它。
如果你知道的话,谢谢!
【问题讨论】:
-
你能分享你从服务器下载数据的 HttpClient 代码吗?我不确定你卡在哪里了。
-
嗨,布兰登,感谢您的关注,我刚刚将我的 HTTPClient 添加到帖子中。 HTTPClient 不下载任何东西,它只是要求我的 PHP 脚本检索我的图片。现在我想将我的 PHP 变量“$blob_data”中的这张图片返回到我的 Android 设备,感谢 HTTPClient 并在本地下载图片,以将其存储在我的 Android 设备中。我希望我更清楚。
-
我想你想添加一个
PHP标签来获得一些帮助,将图像添加到HTTP响应中。 -
谢谢,你是对的,我已经做到了;D
标签: php android httpclient