在编码base64中获取\ n,因此无法转换为JSONObject

时间:2015-06-19 08:20:24

标签: android mysql json string base64

我想编码3张照片并且没有问题,当我想将该字符串发送到PHP并使用JSON在MySQL中保存字符串时,我遇到了问题。 logcat在下面(它只发生在第一张照片中):

  

06-19 14:56:18.141 29570-29586 / com.example.indraaaeff.e_fine2 E / JSON Parser:解析数据时出错org.json.JSONException:Value

然后我尝试在调试器中打印它,我看到String确实包含\n,这意味着<br

这是我打印出来的图片; 我认为这是\n中的encodedString1。希望你能看到\n颜色为蓝色。

http://postimg.org/image/exonizkb3/

MainActivity2.java

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();
    }

}

是否有解决方案使其正确?

0 个答案:

没有答案