Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

Android Studio org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

I'm trying to post some data using volley to a server.

The issue seems to with an encoded string image.

On this line


 String name = namefield.getText().toString();
        String age = agefield.getText().toString();
        String gender = genderfield.getText().toString();
        String color = colorfield.getText().toString();
        String notes = notesfield.getText().toString();
        String images = encodeImage(bitmap);
        updatedetails(name, age, gender, color, notes, String.valueOf(owner), String.valueOf(id), images);

if I change image to empty string


        updatedetails(name, age, gender, color, notes, String.valueOf(owner), String.valueOf(id), "");

then than the data sends to server, which is how I know the image is the problem.

I logged the image response and this is what I get:

Strings: /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
    AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
    AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAUAAk8DASIA
    AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
    AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
    ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
    p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
    AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
    BhJBUQdhcRMiMoEIFEKRobHBCSMzUv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VvCWk/Dz4p2XhbTY7D4PfsffAG3fSNY1ttdIUeH9A1658Py5twhL+DoZA
    yAfEifwPBXxB/wAF3f2U9B/Yk/4J4f8ABDn9huLxBc+Ivg78O/2wPAXgr4la9eLDpum+LNRJn1jW
    tZ1qONtqxzHXfHM0ZbDojudqvH5VeifsXf8ABGf/AIOKP2B7D4myfs//ALX3/BOa18V/F7xXqPjL
    4mfE/wAceFPin8RPi1441WSSWTGu+OPEn7P7+ILi3eWSSYWyMoS4lMrSedtdv2

while the other data response are normal like:


Strings: red


The logcat says the problem is here:


try {
            JSONObject jsonObject = new JSONObject(result);
            Log.i("Check", result);

Here's the relevant code:

public void updatedetails(final String name, final String age, final String gender, final String color, final String notes, final String owner, final String id, final String pet_image) {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Updating pet details");
        progressDialog.show();
        try {
            String url = "https://happy-paws.co.za/dogwalking/apis/v1/pet/update_pet.php";
            StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                    new com.android.volley.Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            String result = response.toString();
//                            Log.d("zzzz","res "+result);
                            Toast.makeText(update.this, response, Toast.LENGTH_SHORT).show();
                            getDataResponse1(result);
                            Log.i("Check", result);
                            Log.i("Check", response);



                        }
                    },
                    new com.android.volley.Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(update.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                            Log.d("TAG", error.getMessage());


                        }
                    }) {
                @Override
                public byte[] getBody() throws com.android.volley.AuthFailureError {
                    String str = "{"name":"" + name + "","age":"" + age + "","gender":"" + gender + "","color":"" + color + "","notes":"" + notes + "","owner":"" + owner + "","id":"" + id + "","pet_image":"" + pet_image + ""}";
                    return str.getBytes();
                }


                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();

//                    params.put("email",email);
//                    params.put("password",password);
                    return params;
                }

            };
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);

        } catch (Exception e) {
            //App.handleUncaughtException(e);
        }

        progressDialog.dismiss();
    }

    public void getDataResponse1(String result) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Please Wait.....");
        progressDialog.show();
        try {
            JSONObject jsonObject = new JSONObject(result);
            Log.i("Check", result);
            JSONObject current = jsonObject.getJSONObject(result);
            String message = current.getString("message");
            String name = jsonObject.isNull("name") ? null : jsonObject.getString("name");
            String age = jsonObject.isNull("age") ? null : jsonObject.getString("age");
            String gender = jsonObject.isNull("gender") ? null : jsonObject.getString("gender");
            String notes = jsonObject.isNull("notes") ? null : jsonObject.getString("notes");
            String color = jsonObject.isNull("color") ? null : jsonObject.getString("color");
//            String android_status=jsonObject.getString("android_status");
            if (message.equals("Pet was updated.")) {
                Toast.makeText(this, "" + message, Toast.LENGTH_SHORT).show();
//                updatedetails(name, age, gender, color, notes, String.valueOf(owner), String.valueOf(id), pet_image);


            } else {
                Toast.makeText(this, "" + message, Toast.LENGTH_SHORT).show();

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        progressDialog.dismiss();
    }

    private void selectImage(Context context) {
        final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Cancel"};

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Choose your profile picture");

        builder.setItems(options, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int item) {

                if (options[item].equals("Take Photo")) {
                    Intent takePicture = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, 0);
                } else if (options[item].equals("Choose from Gallery")) {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, 1);

                } else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_CANCELED) {
            switch (requestCode) {
                case 0:
                    if (resultCode == RESULT_OK && data != null) {
                        bitmap = (Bitmap) data.getExtras().get("data");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

image response is ok. you must set image data type as LONGTEXT or LONGBLOB on database server. but a better solution is to use Multipart Image Upload for upload image on server. you can try with Uploading Images to Server android


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...