类型为java.lang.String的值记录无法转换为JSONObject

时间:2018-08-03 21:06:03

标签: android android-volley

我正在创建一个活动以通过排球更新用户个人资料,但是此错误不断出现

活动

public class ProfileInfoSignup extends AppCompatActivity {

ImageView dp;
ProgressBar pb;
EditText save_name,save_bio;
Button b1;
RequestQueue requestQueue;
boolean IMAGE_STATUS = false;
Bitmap profilePicture;
String name,bio,profile;
String user_email;
private static String SIGNUP_URL = "http://10.0.2.2/app2/profile_ru.php?apicall=updateuser";
SharedPreferences sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_info_signup);

    dp=findViewById(R.id.circleView);
    save_name=findViewById(R.id.edit_name);
    save_bio=findViewById(R.id.edit_bio);
    b1=findViewById(R.id.btn_start);
    pb=findViewById(R.id.progress_bar3);
    sp=getApplicationContext().getSharedPreferences("myPref", Context.MODE_PRIVATE);

        if(sp.contains("user_email")) {
            user_email = sp.getString("user_email", "Data not found");
        }else{
            Toast.makeText(getApplicationContext(), "no email found", Toast.LENGTH_SHORT).show();
        }

    //creating request queue
    requestQueue = Volley.newRequestQueue(this);

    //Adding onClickListener to the ImageView to select the profile Picture
    dp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 1000);
            //result will be available in onActivityResult which is overridden
        }
    });

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name = save_name.getText().toString();
            bio = save_bio.getText().toString();
            if (    //perform validation by calling all the validate functions inside the IF condition
                            validateName(name) &&
                            validateBio(bio)&&
                            validateProfile()
                    ) {
                //Validation Success
                convertBitmapToString(profilePicture);
                saveProfileInfo(SIGNUP_URL,name,bio,user_email);
            }
        }
    });
}

private void saveProfileInfo(String signupUrl, final String getuserName, final String getuserBio, final String getuserEmail) {
    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.POST, signupUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                Toast.makeText(getApplicationContext(), "making new json object and going to another activity", Toast.LENGTH_LONG).show();
                JSONObject jsonObject = new JSONObject(response);

                    if (jsonObject.getBoolean("success")) {
                        Toast.makeText(getApplicationContext(), "Connected to json", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(ProfileInfoSignup.this, Activity2.class);
                        startActivity(intent);
                    } else
                        Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
            }catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> param = new HashMap<String, String>();

            param.put("name", getuserName);
            param.put("bio", getuserBio);
            param.put("email", getuserEmail);
            return param;
        }
    };

    int socketTimeout = 30000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(policy);
    requestQueue.add(stringRequest);
}

private void convertBitmapToString(Bitmap profilePicture) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    profilePicture.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] array = byteArrayOutputStream.toByteArray();
    profile = Base64.encodeToString(array, Base64.DEFAULT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1000 && resultCode == Activity.RESULT_OK && data != null) {
        //Image Successfully Selected
        Toast.makeText(getApplicationContext(), "Image successfully selected", Toast.LENGTH_SHORT).show();
        try {
            //parsing the Intent data and displaying it in the imageview
            Uri imageUri = data.getData();//Geting uri of the data
            InputStream imageStream = getContentResolver().openInputStream(imageUri);//creating an imputstrea
            profilePicture = BitmapFactory.decodeStream(imageStream);//decoding the input stream to bitmap
            dp.setImageBitmap(profilePicture);
            IMAGE_STATUS = true;//setting the flag
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}


private boolean validateName(String string) {
    if (string.equals("")) {
       save_name.setError("Enter Your Name");
        return false;
    } else if (string.length() > 50) {
        save_name.setError("Maximum 50 Characters");
        return false;
    }
    return true;
}

private boolean validateBio(String string) {
    if (string.equals("")) {
      save_bio.setError("Enter Your Email Address");
        return false;
    } else if (string.length() > 300) {
        save_name.setError("Maximum 300 Characters");
        return false;
    }
    return true;
}

private boolean validateProfile() {
    if (!IMAGE_STATUS)
        Toast.makeText(this, "Select A Profile Picture", Toast.LENGTH_SHORT).show();
    return IMAGE_STATUS;
}

}

这是更新活动

每当我单击按钮进行其他活动时,什么都不会发生,并且此错误会记录在日志中

    08-04 14:16:08.023 4409-4409/com.example.user.myapplication I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.
08-04 14:16:08.148 4409-4409/com.example.user.myapplication W/System.err: org.json.JSONException: Value Record of type java.lang.String cannot be converted to JSONObject
08-04 14:16:08.150 4409-4409/com.example.user.myapplication W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
        at org.json.JSONObject.<init>(JSONObject.java:163)
        at org.json.JSONObject.<init>(JSONObject.java:176)
08-04 14:16:08.151 4409-4409/com.example.user.myapplication W/System.err:     at com.example.user.myapplication.Login.ProfileInfoSignup$3.onResponse(ProfileInfoSignup.java:114)
        at com.example.user.myapplication.Login.ProfileInfoSignup$3.onResponse(ProfileInfoSignup.java:109)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
08-04 14:16:08.152 4409-4409/com.example.user.myapplication W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
08-04 14:16:08.153 4409-4409/com.example.user.myapplication W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
08-04 14:16:08.177 1395-1395/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 552960
08-04 14:16:08.184 1395-1395/? I/chatty: uid=1000(system) allocator@2.0-s identical 1 line
08-04 14:16:08.197 1395-1395/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 552960

我知道它与java对象或字符串有关,但我无法弄清楚如何解决它。

0 个答案:

没有答案