我有一个程序可以通过PHP表单登录到服务器(他用用户名和密码填写表单)然后PHP脚本生成一个非常简单的JSON文件:
{
"success":1,
"message":"PHP Login successful!",
"userid":"1",
"data":
[
{
"id_data":"1",
"data_name":"Mona Lisa",
"data_date":"29.9. 2015",
"pic_user_id_user":"1",
"data_picture":‰PNG
IHDR ľ‹ sRGB ®Îé gAMA ±üa pHYs Ä Ä•+ TIDAT(S]NÙ b”FqtGqòªgýB °„¶„¨Ž5—ŽxB¼ædzA»ar
SHÞMÞÐæ5š7Ž†ÿÈ
Œ*Qæ92V¸pRϪü(Ü
…üZI÷Í IEND®B`‚"
},
]
}
这一切都有效,将TextView设置为图像的名称和日期。但是当我想绘制图像时,logCat写入错误SkImageDecoder :: Factory返回null。我不想从任何网址下载图片,我只想从JSON中获取图片。 有公共方法,当登录已经成功,然后设置公共变量,其中是JSON数据。然后在下一个Fragment(list_of_files.java)中使用公共变量(main.java)并在屏幕上写入数据。
// main.java
public static void login_success(JSONObject login_json) throws JSONException, UnsupportedEncodingException {
status.setText("Login successful.");
JSONArray data = login_json.getJSONArray("data"); // get array data witch contains all information about image
int count_of_images = data.length(); // get count, how many images user have
img_name = new String[count_of_images]; // then, create an array, with contains name of each image
img_date = new String[count_of_images]; // and the date of image
// {...} - JSONObject (work with normal methods getting value of coll, f.e. getString( "name of coll" )
// [...] - JSONArray (work with index - numbers)
for (int i = 0; i < count_of_images; i++){
JSONObject under_data = data.getJSONObject(i);
img_name[i] = under_data.getString("data_name");
img_date[i] = under_data.getString("data_date");
if ( i == 0 )
img_img = under_data.getString("data_picture").getBytes();
}
Log.d("== IMAGE SIZE ==", String.valueOf(img_img.length) + " B");
}
// list_of_files.java
// Update listView list_of_files
if (list_of_files_str != null) {
adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, list_of_files_str);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
private FragmentManager supportFragmentManager;
public FragmentManager getSupportFragmentManager() {
return supportFragmentManager;
}
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// set image name and date in textView
TextView image_name = (TextView)getView().findViewById(R.id.image_name);
image_name.setText(list_of_files_str[position] + " // " + date_str[position]);
// draw image with char array from main class Red Orchestra
ImageView img = (ImageView)getView().findViewById(R.id.imageView);
Bitmap image_to_draw;
byte[] data;
String str = "";
try {
data = str.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
byte[] imageAsBytes = Base64.decode(base64.getBytes(), Base64.URL_SAFE);
image_to_draw = BitmapFactory.decodeByteArray(data, 0, data.length);
img.setImageBitmap( image_to_draw );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
} else { // if the user is not login, write him message
String[] must_login = new String[] {"You must be sign in."};
adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, must_login);
list.setAdapter(adapter);
}
// Inflate the layout for this fragment
return rootView;
}
我真的不知道,如何保存这个问题。我搜索了不同的论坛和网站,但没有任何效果。
PHP文件:
//load and connect to MySQL database stuff
需要( “config.inc.php中”); $ login_ok = false;
if(!empty($ _ POST)){ $ query_1 =“SELECT * FROM pic_user WHERE user_name =:username”;
$query_params = array(
':username' => $_POST['username']
);
try {
$stmt = $db->prepare($query_1);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "PHP Database Error1. Please Try Again!";
die(json_encode($response));
}
$validated_info = false;
$row = $stmt->fetch();
if ($row) {
if ($_POST['password'] === $row['user_password']) {
$login_ok = true;
}
}
if ($login_ok) {
$query_2 = "SELECT * FROM pic_data WHERE pic_user_id_user = :userid";
$user_id = $row['id_user'];
$user_name = $row['user_name'];
$query_params_2 = array( ':userid' => $user_id );
$stmt = $db->prepare($query_2);
$result = $stmt->execute($query_params_2);
$row = $stmt->fetchAll();
$response["success"] = 1;
$response["message"] = "PHP Login successful!";
$response["userid"] = $user_id;
$response['data'] = $row;
die(json_encode($response));
} else {
$response["success"] = 0;
$response["message"] = "PHP Invalid Credentials!";
die(json_encode($response));
}
答案 0 :(得分:0)
我明白了!有没有错误的base64转换。当我删除代码中的转换并将图像转换为base64(http://www.motobit.com/util/base64-decoder-encoder.asp)并将base64字符串添加到数据库中而不是通过JSON下载它时,它可以工作。