我有一个带有MySQL的远程数据库,并且将我的应用程序用户的照片存储在数据库中,作为LONGTEXT类型的数据库的一行。
我使用Base64将照片转换为字符串。
我使用JSON和PHP连接到远程数据库,因此,我必须使用Base64,因为据我所知,JSON和PHP需要在参数上发送字符串,而使用Base64可以将照片转换为字符串。
可以,但是非常慢。当我加载100 KB的照片时,会花费很多时间,但是当我加载5 KB的照片时,则只需要两到三秒钟。
一位朋友告诉我使用BLOB代替Base64,但是如何将BLOB与JSON和与数据库的PHP连接一起使用?另外,我需要将图像存储在表的一行上USER。这是因为用户没有特权将文件上传到远程服务器,但是他们可以通过将照片作为字符串上传到表的行中来上传照片USER。
谢谢
编辑:
这是需要花大量时间等待的代码(它在以下行中等待:while ((line = reader.readLine()) != null)
{,它正在等待reader.readLine() )
此代码从远程数据库中获取一个用户,这需要很长时间才能在我的应用程序上显示该用户
public Friend RetrieveOneUser(String email)
{
Friend friend=null;
String result = "";
//the parameter data to send
ArrayList nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("email",email));
//http post
InputStream is=null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(this.BaseURL + this.GetOneUser_URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i
{
JSONObject json_data = jArray.getJSONObject(i);
friend=new Friend(json_data.getString("email"),json_data.getString("password"), json_data.getString("fullName"), json_data.getString("mobilePhone"), json_data.getString("mobileOperatingSystem"),"",json_data.getString("photo"));
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return friend;
}