我在phpmyadmin数据库中有 USERS 表。在表中我有一个列,其中包含用户个人资料图片的链接。但我认为我上传的图像并不好。当我打开它时,我看到一个像这样的小方块:
编辑:图片部分已修复
当我试图用piccaso获取它时,我什么都没得到。 我上传图片的方式是将其编码为字符串,然后在register.php中对其进行解码
这是将图片转换为字符串的方法:
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
这是我上传图片的php:
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$passWord = $_POST['password'];
$phoneNumber = $_POST['phonenumber'];
$apartmentNumber = $_POST['apartmentNumber'];
$image = $_POST['image']; //this is the encoded string
//upload image to image folder in the server
$upload_folder = "upload";
$path = "$upload_folder/$phoneNumber.jpeg";
if(file_put_contents($path, base64_encode($image)) != flase){
echo "uploaded";
}
$image_link = "my url/$path";
$result = mysqli_query($con,"INSERT INTO Users (firstName, lastname, password, phone , apartmentNum,image)
VALUES ('$firstName', '$lastName', '$EncryptPassword', '$phoneNumber', '$apartmentNumber' ,'$image_link')");
这是我在创建json对象的php:
$sql = ("select * from Users where phone = '$phone' and password = '$password' ");
$check = mysqli_fetch_array(mysqli_query($con,$sql));
if(isset($check)){
$hasApartment = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($hasApartment);
$apartmentNumber = $row['apartmentNum'];
$firstName = $row['firstName'];
$lastName = $row['lastname'];
$image = $row['image'];
echo '{"query_result":"SUCCESS", "apartmentNumber":' . $apartmentNumber.' , "firstName":' . trim($firstName) .' ,"lastName":' . trim($lastName).', "image":' . $image.'}';
}
我得到了这个例外:
org.json.JSONException: Unterminated object at character 102 of {"query_result":"SUCCESS", "apartmentNumber":54 , "firstName":a ,"lastName":a, "image":XXXXXX.XX.XX/upload/112.jpeg}
这是json对象:
{" query_result":" SUCCESS"," apartmentNumber":54," firstName":a," lastName&# 34;:a," image":XXXXXX.XX.XX / upload / 112.jpeg}
char 102它的" u"在上传这是我做GET请求的时间。在onResponse()中,我尝试将json文本作为json对象处理:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String query_result = response.getString("query_result");
switch (query_result){
case "SUCCESS": // user name and password exists and mathces and user associated with apartment
int apartmentNum = Integer.parseInt(response.get("apartmentNumber").toString());
Intent intent = new Intent(LoginActivityScreen.this , HomeActivityScreen.class);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("phoneNumber" , PhoneValidationActivity.PHONE_NUMBER);
editor.putString("firstName", response.getString("firstName"));
editor.putString("lastName", response.getString("firstName"));
editor.putBoolean("loggedIn", true);
editor.putInt("apartmentNumber", apartmentNum);
Bitmap profilePicture = Picasso.with(context).load("http://roomates.96.lt/upload/0547517307.jpeg").get();
editor.apply();
startActivity(intent);
break;
case "FAILURE": //user name or password are incorrect
Toast.makeText(getApplicationContext(), "wrong name or password", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
} catch (JSONException e) { //this exception is caught
e.printStackTrace();
Log.e("e" , e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.e("e", e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getInstance(context).addToRquestQueue(jsonObjectRequest);
}
答案 0 :(得分:0)
String query_result = response.getString("query_result")
您是否看到"query_result":"SUCCESS"
和"image":XXXXXX.XX.XX/upload/112.jpeg
之间存在差异?
语法上?
答案 1 :(得分:0)
如果您在图像路径上遇到JSON响应错误,那么您应该只需 json_encode 您的响应,这样您的所有特殊字符都将被正确转义。
在您的JSON响应中,您的图片路径 XXXXXX.XX.XX / upload / 112.jpeg 应该像 XXXXXX.XX.XX / upload / 112.jpeg
希望它对你有所帮助。