java - 无法使用Android在mysql中插入数据

标签 java android xml android-studio xml-parsing

我正在尝试在 mysql 中插入注册数据。 我正在使用 PHP 文件来连接 MYSQL.. 引用:http://sampleprogramz.com/android/mysqldb.php#

有了这个,我可以使用 PHP 成功地将我的数据保存在数据库中 但现在我想使用 java 而不是 php...我无法将其保存在 db 中

我的代码:

 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();
    }

现在我想使用 java webservice...为此,我应该将此 ArrayList 转换为 Xmlstring ..

请帮助我,如何将 nameValuePairs 转换为 XMLString...通过它,我可以获取 StringEntity 并在 webservice 中传递数据...提前谢谢

最佳答案

您不能像使用 php 那样调用 java-webserviec。你必须使用 soap 对象来实现它我认为这个链接可能对你有帮助。 java webservice

关于java - 无法使用Android在mysql中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29314299/

相关文章:

java - 将自定义信息添加到 JBehave HTML 报告

java - 使用 ReentrantLock 和队列的生产者消费者

java - ArrayList 中的图形

android - Icecast:在 Android 上开始播放时暂停

java - 使用 Android Intent 共享文件 : File extension is removed

android - 具有多个 Android 客户端的服务器

php - 从 XML 标签获取 HTML

java - SQL预编译语句

java - 如何传递 XML Document 对象?

c# - 了解 linq 中使用的 xml 命名空间