Cannot Insert data in mysql using Android

2019-08-05 17:34发布

问题:

I am trying to insert registration data in mysql . I was using PHP file, to connect with MYSQL.. Reference: http://sampleprogramz.com/android/mysqldb.php#

With this , I can successfully save my data in database using PHP but now I want to use java instead of php...I couldn't save it in db

My code :

 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("registerId",""));
    nameValuePairs.add(new BasicNameValuePair("registerName",reg_name));
    nameValuePairs.add(new BasicNameValuePair("regContactNo",contactno));
    nameValuePairs.add(new BasicNameValuePair("regEmailAddr",emailaddr));
    nameValuePairs.add(new BasicNameValuePair("registerDesignation",designation));
    nameValuePairs.add(new BasicNameValuePair("regCmpnyName",company_name));
    nameValuePairs.add(new BasicNameValuePair("evtCd",eventCode));
    StrictMode.setThreadPolicy(policy);

    //http post
    try{
        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost("http://url/useracc/register");

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("Response:" , response.toString());
        Log.e("is",""+is);

        Log.e("log_tag", "connection success ");
        Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();

    }

    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection "+e.toString());
        Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

    }
    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null)
        {
            /*sb.append(line + "\n");
            Intent i = new Intent(getBaseContext(),DatabaseActivity.class);
            startActivity(i);*/
        }
        is.close();

        result=sb.toString();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error converting result "+e.toString());
    }

    try{

        JSONObject json_data = new JSONObject(result);

        CharSequence w= (CharSequence) json_data.get("re");

        Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();


    }
    catch(JSONException e)
    {
        Log.e("log_tag", "Error parsing data "+e.toString());
       // Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
    }

Now I want to work with java webservice...and for it , I should convert this ArrayList to Xmlstring ..

Please help me for this, How can I convert nameValuePairs to XMLString...Through it , I can take StringEntity and pass data in webservice...Thx in advance

回答1:

you can't call java-webserviec just like that you do with php. you have to implement it using soap object i think this link might be helpful to you. java webservice