0

Hi i want to fetch the selected column from mysql by matching one column name i.e

"select column_name from day where date='$date'"

But it is giving me serious error , i have check the connectivity issues they are fine , i think there is some problem in code please help me

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONObject;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class Day extends Activity {

        String dt="";
        String mc="";
        String bc="";
        ImageView dateimg;
        String name;
        String date;
        InputStream is=null;
        String result=null;
        String line=null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.day_lay);
            getCurrDate();
            select();
            dateimg = (ImageView) findViewById(R.id.img);



            dateimg.setOnClickListener(new OnClickListener()
            {
                    public void onClick(View v)
                     {  
                        //getCurrDate();

                        }

            });



        }


        public String getCurrDate()
        {

            Date cal = Calendar.getInstance().getTime();
            dt = cal.toLocaleString();

            mc = dt.substring(0, dt.length() - 17);
            bc=mc.substring(4);
            System.out.println("Date is"+bc);
            date=bc;
            return dt;

        }


         public void select()
            {
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("date",date));

                try
                {
   HttpClient httpclient = new DefaultHttpClient()    
   HttpPost httppost = new HttpPost("http://192.168.1.1/wpcontent/themes/app/day.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost); 
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                    Log.e("pass 1", "connection success ");
            }
                catch(Exception e)
            {
                    Log.e("Fail 1", e.toString());
                    Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();
            }     

                try
                {
                    BufferedReader reader = new BufferedReader
                        (new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb = new StringBuilder();
                        while ((line = reader.readLine()) != null)
                {
                        sb.append(line + "\n");
                    }
                        is.close();
                        result = sb.toString();
                    Log.e("pass 2", "connection success ");
            }
                catch(Exception e)
                {
                Log.e("Fail 2", e.toString());
            }     

            try
                {
                    JSONObject json_data = new JSONObject(result);
                    name=(json_data.getString("name"));
                Toast.makeText(getBaseContext(), "Name : "+name,
                    Toast.LENGTH_SHORT).show();
                }
                catch(Exception e)
                {
                    Log.e("Fail 3", e.toString());
                }

            System.out.println("Got Value"+name);
            }

    }

// Database select

<?php
    $host='127.0.0.1';
    $uname='root';
    $pwd='password';
    $db="android";

    $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
    mysql_select_db($db,$con) or die("db selection failed");

    $date=$_REQUEST['date'];

    $r=mysql_query("select mat from vicks where date='$date'",$con);

    while($row=mysql_fetch_array($r))
    {
        $flag[name]=$row[name];
    }

    print(json_encode($flag));
    mysql_close($con);
?>

//Logcat

05-08 14:04:34.938: I/dalvikvm-heap(995): Grow heap (frag case) to 54.839MB for 2312016-byte allocation
05-08 14:04:35.218: D/dalvikvm(995): GC_CONCURRENT freed 1K, 4% free 56085K/58055K, paused 11ms+23ms
05-08 14:04:35.758: D/dalvikvm(995): GC_FOR_ALLOC freed 564K, 4% free 56086K/58055K, paused 172ms
05-08 14:04:35.788: I/dalvikvm-heap(995): Grow heap (frag case) to 57.044MB for 2312016-byte allocation
05-08 14:04:36.538: D/dalvikvm(995): GC_CONCURRENT freed <1K, 4% free 58344K/60359K, paused 11ms+98ms
05-08 14:08:30.258: I/System.out(995): Date is8
05-08 14:08:30.298: E/Fail 1(995): android.os.NetworkOnMainThreadException
05-08 14:08:30.338: E/Fail 2(995): java.lang.NullPointerException
05-08 14:08:30.338: E/Fail 3(995): java.lang.NullPointerException
05-08 14:08:30.338: I/System.out(995): Got valuenull
raj
  • 388
  • 5
  • 24

1 Answers1

0

This is not about connectivity. This is about handling network activity on the main thread.

You can either disable that warning (which is bad practice) or perform that task in a separate thread.

A common Android pattern is using an AsyncTask for that. The AsyncTask class uses three paramters in its constructor: params, progress, result. In your case they can all be null if you only need to run something in the background.

    private class DatabaseTask extends AsyncTask<void, void, void> 
    {
         protected void doInBackground(Void... params) 
         {
             // YOUR DB CODE
         }
    }

And execute like this: new DatabaseTask().execute();

Or, you can change the first void to whatever type you need if you want to use input parameters. Read more here.

Eran Goldin
  • 980
  • 12
  • 21
  • ok let me try and thanks for the error but can you help me how to put this in Asynctask coz m not familiar with it . It will be a great help from your side – raj May 24 '14 at 08:55