如何解决错误:java.lang.String类型的值UPDATE无法转换为JSONObject

时间:2016-08-08 07:50:43

标签: php android mysql

我正在开发一个应用程序我使用两个微调器当我点击微调项目时所选项目应该插入到mysql数据库中,我使用下面的代码,但它给出错误,即“类型更新java.lang.String类型无法在“onPostExecute”方法中的SignupActivity1.java页面上转换为JSONObject。 我该如何解决这个问题? 请建议我。

SpinnerActivity .java

public class SpinnerActivity extends Activity {

        Context ctx = this;
        String ride_root, cab_type, email;
        private boolean loggedIn = false;
        TextView textView, textView1, textView2;

        Spinner spinner1, spinner2;
        Button submit;
        private ArrayList<String> students1;
        private ArrayList<String> students2;
        private JSONArray result1, result2, result;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_spinner);
            submit = (Button) findViewById(R.id.button2);
            textView=(TextView)findViewById(R.id.textView3_user);
           // textView1=(TextView)findViewById(R.id.textView3_s1);
           // textView2=(TextView)findViewById(R.id.textView3_s2);

            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);
                    String email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF,"Not Available");
                    //Showing the current logged in email to textview
                    // textView.setText("Current User: " + email);
                    textView.setText(email);


                    if(loggedIn){
                        //We will start the Profile Activity

                        submitForm();
                        Intent intent = new Intent(SpinnerActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }

                }
            });

            final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
            ArrayAdapter<String> sAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.rides));
            spinner1.setAdapter(sAdapter1);


            final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
            ArrayAdapter<String> sAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.cabtype));
            spinner2.setAdapter(sAdapter2);

            spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    ride_root = spinner1.getSelectedItem().toString();
                  //  textView1.setText(ride_root);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                   cab_type = spinner2.getSelectedItem().toString();
                  //  textView2.setText(cab_type);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }

        private void submitForm() {

            email=textView.getText().toString();
            Toast.makeText(this, "Signing up...", Toast.LENGTH_SHORT).show();
            new SignupActivity1(this).execute(ride_root,cab_type,email);
        }
    }

SignupActivity1.java

    public class SignupActivity1 extends AsyncTask<String, Void, String> {

        private Context context;
        Boolean error, success;

        public SignupActivity1(Context context) {
            this.context = context;
        }

        protected void onPreExecute() {

        }

        @Override
        protected String doInBackground(String... arg0) {
            String ride_root = arg0[0];
            String cab_type = arg0[1];
            String email = arg0[2];

            String link;
            String data;
            BufferedReader bufferedReader;
            String result;

            try {

                data = "?ride_root=" + URLEncoder.encode(ride_root, "UTF-8");
                data += "&cab_type=" + URLEncoder.encode(cab_type, "UTF-8");
                data += "&email=" + URLEncoder.encode(email, "UTF-8");

                link = "http://example.in/Spinner/spinnerU.php" + data;

                URL url = new URL(link);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();

                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                result = bufferedReader.readLine();
                return result;

            } catch (Exception e) {
                // return new String("Exception: " + e.getMessage());
                // return null;
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            String jsonStr = result;
            if (jsonStr != null) {
                try {

                    JSONObject jsonObj = new JSONObject(jsonStr);
                    String query_result = jsonObj.getString("query_result");
                    if (query_result.equals("SUCCESS")) {
                        Toast.makeText(context, "Success! Your are Now MangoAir User.", Toast.LENGTH_LONG).show();

                    } else if (query_result.equals("FAILURE")) {
                        Toast.makeText(context, "Looks Like you already have Account with US.", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    // Toast.makeText(context, "Error parsing JSON Please data Fill all the records.", Toast.LENGTH_SHORT).show();
                    // Toast.makeText(context, "Please LogIn", Toast.LENGTH_SHORT).show();
                    Toast.makeText(context, "Please Login", Toast.LENGTH_LONG).show();

                }
            } else {
                Toast.makeText(context, "Grrr! Check your Internet Connection.", Toast.LENGTH_SHORT).show();
            }
        }

    }

php文件     

$con=mysqli_connect("localhost","user","pass","db");
$id=$_GET["email"];
$ride_root=$_GET["ride_root"];
$cab_type=$_GET["cab_type"]; 

$sql = "UPDATE tbl_users SET ride_root='$ride_root', cab_type='$cab_type' WHERE id='$id'";
// echo $sql;
// $r=mysqli_query($con, $sql);

                if(mysqli_query($con,$sql)){
                    echo '{"query_result":"SUCCESS"}';
                }else{
                    '{"query_result":"FAILURE"}';
                }

            mysqli_close($con);

?>

2 个答案:

答案 0 :(得分:1)

在onPost中试试这个:

Log.e("JSON respons",String.valueof(jsonstr));
Log.e("JSON respons",String.valueof(query_result));

答案 1 :(得分:0)

首先,你必须在logcat中检查jsonStr以查看你的Url响应的格式。要么是JASONArray形式,要么是JASONObject形式。之后,你必须在onPost中获取数据。

我认为您的Response不是JASONObject形式。