blob转成json js_如何将BLOB与JSON和PHP结合使用?

我有一个带有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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值