我的数据无法上传到mysql服务器数据库,显示字符串无法转换为JSONobject

时间:2019-05-15 22:03:31

标签: java php android

我正在创建一个简单的android应用程序,用于将数据上传到mysql服务器数据库,但由于无法将字符串转换为JSON对象而显示错误。 我也可以过滤粗俗单词吗?

这是我的android应用程序,如果可以正常工作,我会用它来上传字典中的单词和含义

我尝试了其他解决方案。

public class MainActivity extends Activity {

private EditText etName, etMobile, etAddress;
private Button btnSubmit;
private ProgressDialog pDialog;
private JSONObject json;
private int success = 0;
private HTTPURLConnection service;
private String strname = "", strMobile = "", strAddress = "";
//Initialize webservice URL
private String path = "https://meiteiwords.000webhostapp.com/add_employee.php";

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

    etName = (EditText) findViewById(R.id.etName);
    etMobile = (EditText) findViewById(R.id.etMobile);
    etAddress = (EditText) findViewById(R.id.etAddress);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    //Initialize HTTPURLConnection class object
    service = new HTTPURLConnection();

    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!etName.getText().toString().equals("") && !etMobile.getText().toString().equals("")) {
                strname = etName.getText().toString();
                strMobile = etMobile.getText().toString();
                strAddress = etAddress.getText().toString();
                //Call WebService
                new PostDataTOServer().execute();
            } else {
                Toast.makeText(getApplicationContext(), "Please Enter all fields", Toast.LENGTH_LONG).show();
            }
        }
    });
}

private class PostDataTOServer extends AsyncTask<Void, Void, Void> {

    String response = "";
    //Create hashmap Object to send parameters to web service
    HashMap<String, String> postDataParams;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        postDataParams = new HashMap<String, String>();
        postDataParams.put("name", strname);
        postDataParams.put("mobile", strMobile);
        postDataParams.put("address", strAddress);
        //Call ServerData() method to call webservice and store result in response
        response = service.ServerData(path, postDataParams);
        try {
            json = new JSONObject(response);
            //Get Values from JSONobject
            System.out.println("success=" + json.get("success"));
            success = json.getInt("success");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        if (success == 1) {
            Toast.makeText(getApplicationContext(), "Employee Added successfully..!", Toast.LENGTH_LONG).show();
        }
    }
}

}

这是我的php代码显示警告:

https://pastebin.com/P50EWbbR https://pastebin.com/Aut7fFq2

1 个答案:

答案 0 :(得分:0)

由于尝试将String转换为JSONObject,因此出现类型转换错误。您未能将响应作为JSONObject发送。