Android 正在获取\n编码base64,因此无法将其转换为JSONObject
我想对3张照片进行编码,没有问题,当我想将该字符串发送到PHP并使用JSON将该字符串保存在MySQL中时,我遇到了问题。日志如下(仅在第一张照片中出现): 06-19 14:56:18.141 29570-29586/com.example.indraaaeff.e_fine2 e/JSON解析器﹕ 分析数据org.json.JSONException:Value时出错Android 正在获取\n编码base64,因此无法将其转换为JSONObject,android,mysql,json,string,base64,Android,Mysql,Json,String,Base64,我想对3张照片进行编码,没有问题,当我想将该字符串发送到PHP并使用JSON将该字符串保存在MySQL中时,我遇到了问题。日志如下(仅在第一张照片中出现): 06-19 14:56:18.141 29570-29586/com.example.indraaaeff.e_fine2 e/JSON解析器﹕ 分析数据org.json.JSONException:Value时出错 然后我尝试在debugger中打印出来,我看到字符串确实包含\n,这意味着添加一些代码以便我们可以提供帮助。请添加log c
然后我尝试在debugger中打印出来,我看到字符串确实包含
\n
,这意味着添加一些代码以便我们可以提供帮助。请添加log cat exception我不确定为什么要对base64中的内容进行json编码。。。但是,您可以只对字符串进行json编码吗?在编码它之前,它不应该被包装在数组或散列中吗?我在那里添加了log cat,ajitso先生,我不应该将字符串包装在数组或散列中?你能用代码sir@Michael sqlbot解释一下吗?
private void encodeFoto1(Uri uriFoto1) {
File imageFile1 = new File(uriFoto1.getPath());
Bitmap bm1 = BitmapFactory.decodeFile(imageFile1.getPath());
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
bm1.compress(Bitmap.CompressFormat.PNG, 50, baos1);
byte[] byte_arr1 = baos1.toByteArray();
encodedString1 = Base64.encodeToString(byte_arr1, 1);
}
private void encodeFoto2(Uri uriFoto2) {
File imageFile2 = new File(uriFoto2.getPath());
Bitmap bm2 = BitmapFactory.decodeFile(imageFile2.getPath());
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
bm2.compress(Bitmap.CompressFormat.PNG, 50,baos2);
byte[] byte_arr2 = baos2.toByteArray();
encodedString2 = Base64.encodeToString(byte_arr2, 2);
}
private void encodeFoto3(Uri uriFoto3) {
File imageFile3 = new File(uriFoto3.getPath());
Bitmap bm3 = BitmapFactory.decodeFile(imageFile3.getPath());
ByteArrayOutputStream baos3 = new ByteArrayOutputStream();
bm3.compress(Bitmap.CompressFormat.PNG, 50, baos3);
byte[] byte_arr3 = baos3.toByteArray();
encodedString3 = Base64.encodeToString(byte_arr3, 3);
}
public class kirimData extends AsyncTask<String, Bundle, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity2.this);
pDialog.setMessage("Sending Data...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("foto_sim",encodedString1));
params.add(new BasicNameValuePair("foto_stnk",encodedString2));
params.add(new BasicNameValuePair("foto_sitaan",encodedString3));
Log.d("request!", "starting");
JSONObject json = parserRegister.makeHttpRequest(
send_form_url, "POST", params);
Log.d("url", send_form_url);
// full json response
Log.d("register", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Data Sent!", json.toString());
// Pakai method runOnUiThread jika ingin memulai intent baru
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent i = new Intent(getApplicationContext(), MenuActivity.class);
finish();
startActivity(i);
}
});
return json.getString(TAG_MESSAGE);
}else{
Log.d("Sending Failed!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
}
}