使用android应用程序不会将数据插入到mysql数据库中

时间:2016-04-12 16:20:35

标签: java android

Java代码......!

public void saveDB(View v)
{
    //BackgroundTask Class Execution

    name= ""+output1.getText().toString();
    batch_no= ""+output2.getText().toString();
    price= ""+output3.getText().toString();
    mfg_dt= ""+output4.getText().toString();
    exp_dt= ""+output5.getText().toString();

    insertintoDB();

    onBackPressed();
}

public void insertintoDB()
{
    //setting name value pairs
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    //Adding the variables inside the NameValuePairs

    nameValuePairs.add(new BasicNameValuePair("name",name));
    nameValuePairs.add(new BasicNameValuePair("batch_no",batch_no));
    nameValuePairs.add(new BasicNameValuePair("price",price));
    nameValuePairs.add(new BasicNameValuePair("mfg_dt",mfg_dt));
    nameValuePairs.add(new BasicNameValuePair("exp_dt",exp_dt));

    //Setting up the connection inside the try and catch block

    try
    {
       //Setting up the default http client
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://illusion.netau.net/android/saveData.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Getting the response
        HttpResponse response = httpclient.execute(httppost);
        //Setting up the entity
        HttpEntity entity = response.getEntity();

        //Setting up the content inside the input stream reader
        is = entity.getContent();

       /* URL url=new URL("http://illusion.netau.net/android/saveData.php");
        urlConnection=(HttpURLConnection) url.openConnection();
        urlConnection.connect();
        is=urlConnection.getInputStream();*/

        Toast.makeText(getApplicationContext(), "Data Entered Successfully...",Toast.LENGTH_LONG).show();
        Log.e("Conn_Fine", "connection success ");
    }catch (ClientProtocolException e)
    {
        Log.e("Client Protocol","Log_tag");
        e.printStackTrace();
    }
    catch(IOException e)
    {
        Log.e("Log_tag","IOException");
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "No Internet Access",Toast.LENGTH_LONG).show();
    }


}

这是我的java代码,我使用httpClient ....!

将数据保存到mysql数据库中

-------------------- LOGCAT消息.------------------------ ---

04-12 20:56:57.473 2189-2189/com.example.fezimirza.layoutexample I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@713eb3c time:19721431
04-12 20:57:01.734 2189-2189/com.example.fezimirza.layoutexample E/pass 1: connection success 
04-12 20:57:01.743 2189-2189/com.example.fezimirza.layoutexample I/Timeline: Timeline: Activity_launch_request id:com.example.fezimirza.layoutexample time:19725701
04-12 20:57:01.763 2189-2189/com.example.fezimirza.layoutexample I/Choreographer: Skipped 41 frames!  The application may be doing too much work on its main thread.
04-12 20:57:02.057 2189-2189/com.example.fezimirza.layoutexample I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1932a91 time:19726015

3 个答案:

答案 0 :(得分:0)

此处没有任何代码负责写入数据库。看起来您正在对某些API进行API调用(我猜是负责写入数据库)。如果是这种情况,那么您应该只是检查以确保API调用成功并使用正确的POST正文进行,如果是,请调试您的API代码以找出原因为什么&# 39;正确写入数据库。

答案 1 :(得分:0)

  1. 正如zec和NguyễnTrungHiếu所提到的,服务器调用应该在使用AsyncTask的辅助线程中进行。如果您是Android的新手,请检查此.. http://developer.android.com/reference/android/os/AsyncTask.html

  2. 您发送到服务器的参数。您的服务器接受哪种格式。服务器端响应无法反映在Android代码中。检查php部分接收数据。

答案 2 :(得分:-1)

试一试!!!!!

public void saveDB(View v)
{




new insertintoDB().execute();
}  

class insertintoDB extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        /*
         * pDialog = new ProgressDialog(MainActivity.this);
         * pDialog.setMessage("Please wait..."); pDialog.show();
         */
    }

@Override
    protected String doInBackground(String... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://illusion.netau.net/android/saveData.php");

        try {
             name= ""+output1.getText().toString();
             batch_no= ""+output2.getText().toString();
             price= ""+output3.getText().toString();
             mfg_dt= ""+output4.getText().toString();
             exp_dt= ""+output5.getText().toString();
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("name",name));
            nameValuePairs.add(new BasicNameValuePair("batch_no",batch_no));
            nameValuePairs.add(new BasicNameValuePair("price",price));
            nameValuePairs.add(new BasicNameValuePair("mfg_dt",mfg_dt));
            nameValuePairs.add(new BasicNameValuePair("exp_dt",exp_dt));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        return null;
    }

 @Override
    protected void onPostExecute(String s) {
        /*
         * if (pDialog.isShowing()) { pDialog.dismiss();
         * Toast.makeText(getApplication(), "Complete",
         * Toast.LENGTH_LONG).show(); }
         */
    }
 }