0

I have design my database on MySQL and link it through php. how can i get it on android application by using HTTP GET method??? i used JSON Parsing for this purpose to get data from MYSQL but i fails to display:(( Now i want to get data from HTTP GET request so how can i use it?? does i upload my database on server??? how can i upload it?? and what is the method for it. i'll be thankful to you for help

MySQL database is:

    <?php
// read_My_Task.php


/*
 * Following code will get single task details
 * A task_detail is identified by task id (cid)
 */
 // array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["cid"])) {
    $cid = $_GET['cid'];

    // get a task from my-task table
    $result = mysql_query("SELECT *FROM my_task WHERE cid = $cid");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $my_task = array();
            $my_task["cid"] = $result["cid"];
            $my_task["cus_name"] = $result["cus_name"];
            $my_task["contact_number"] = $result["contact_number"];
            $my_task["ticket_no"] = $result["ticket_no"];
            $my_task["task_detail"] = $result["task_detail"];

            // user node
            $response["my_task"] = array();

            array_push($response["my_task"], $my_task);
            // echoing JSON response
            echo json_encode($response);         
             }            

        else {
            // no task found
            $response["success"] = 0;
            $response["message"] = "No task found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no task found
        $response["success"] = 0;
        $response["message"] = "No task found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}

?>

LogCat errors are:

12-17 11:00:39.180: W/System.err(10667): org.apache.http.conn.ConnectTimeoutException: Connect to /198.168.0.29:80 timed out
12-17 11:00:39.180: W/System.err(10667):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
12-17 11:00:39.180: W/System.err(10667):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
12-17 11:00:39.180: W/System.err(10667):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-17 11:00:39.180: W/System.err(10667):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-17 11:00:39.180: W/System.err(10667):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
12-17 11:00:39.180: W/System.err(10667):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-17 11:00:39.180: W/System.err(10667):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-17 11:00:39.180: W/System.err(10667):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-17 11:00:39.180: W/System.err(10667):    at com.example.visit.record.JSONParser.makeHttpRequest(JSONParser.java:62)
12-17 11:00:39.180: W/System.err(10667):    at com.example.visit.record.My_Task$GetTaskDetails.doInBackground(My_Task.java:170)
12-17 11:00:39.180: W/System.err(10667):    at com.example.visit.record.My_Task$GetTaskDetails.doInBackground(My_Task.java:1)
12-17 11:00:39.180: W/System.err(10667):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-17 11:00:39.180: W/System.err(10667):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-17 11:00:39.180: W/System.err(10667):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-17 11:00:39.180: W/System.err(10667):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-17 11:00:39.180: W/System.err(10667):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-17 11:00:39.190: W/System.err(10667):    at java.lang.Thread.run(Thread.java:1027)
12-17 11:00:39.190: E/Buffer Error(10667): Error converting result java.lang.NullPointerException
12-17 11:00:39.190: E/JSON Parser(10667): Error parsing data org.json.JSONException: End of input at character 0 of 
12-17 11:00:39.190: W/dalvikvm(10667): threadid=9: thread exiting with uncaught exception (group=0x400205a0)
12-17 11:00:39.200: E/AndroidRuntime(10667): FATAL EXCEPTION: AsyncTask #1
12-17 11:00:39.200: E/AndroidRuntime(10667): java.lang.RuntimeException: An error occured while executing doInBackground()
12-17 11:00:39.200: E/AndroidRuntime(10667):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at java.lang.Thread.run(Thread.java:1027)
12-17 11:00:39.200: E/AndroidRuntime(10667): Caused by: java.lang.NullPointerException
12-17 11:00:39.200: E/AndroidRuntime(10667):    at com.example.visit.record.My_Task$GetTaskDetails.doInBackground(My_Task.java:174)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at com.example.visit.record.My_Task$GetTaskDetails.doInBackground(My_Task.java:1)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-17 11:00:39.200: E/AndroidRuntime(10667):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-17 11:00:39.200: E/AndroidRuntime(10667):    ... 4 more
12-17 11:00:39.510: E/WindowManager(10667): Activity com.example.visit.record.My_Task has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054caf0 that was originally added here
12-17 11:00:39.510: E/WindowManager(10667): android.view.WindowLeaked: Activity com.example.visit.record.My_Task has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054caf0 that was originally added here
12-17 11:00:39.510: E/WindowManager(10667):     at android.view.ViewRoot.<init>(ViewRoot.java:277)
12-17 11:00:39.510: E/WindowManager(10667):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
12-17 11:00:39.510: E/WindowManager(10667):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-17 11:00:39.510: E/WindowManager(10667):     at android.view.Window$LocalWindowManager.addView(Window.java:433)
12-17 11:00:39.510: E/WindowManager(10667):     at android.app.Dialog.show(Dialog.java:288)
12-17 11:00:39.510: E/WindowManager(10667):     at com.example.visit.record.My_Task$GetTaskDetails.onPreExecute(My_Task.java:149)
12-17 11:00:39.510: E/WindowManager(10667):     at android.os.AsyncTask.execute(AsyncTask.java:391)
12-17 11:00:39.510: E/WindowManager(10667):     at com.example.visit.record.My_Task.onCreate(My_Task.java:108)
12-17 11:00:39.510: E/WindowManager(10667):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
12-17 11:00:39.510: E/WindowManager(10667):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
12-17 11:00:39.510: E/WindowManager(10667):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
12-17 11:00:39.510: E/WindowManager(10667):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
12-17 11:00:39.510: E/WindowManager(10667):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
12-17 11:00:39.510: E/WindowManager(10667):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 11:00:39.510: E/WindowManager(10667):     at android.os.Looper.loop(Looper.java:143)
12-17 11:00:39.510: E/WindowManager(10667):     at android.app.ActivityThread.main(ActivityThread.java:4268)
12-17 11:00:39.510: E/WindowManager(10667):     at java.lang.reflect.Method.invokeNative(Native Method)
12-17 11:00:39.510: E/WindowManager(10667):     at java.lang.reflect.Method.invoke(Method.java:507)
12-17 11:00:39.510: E/WindowManager(10667):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-17 11:00:39.510: E/WindowManager(10667):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-17 11:00:39.510: E/WindowManager(10667):     at 

it gives error:

Status Code: 404 Not Found
Accept-Ranges: bytes
Content-Language: en
Content-Type: text/html; charset=iso-8859-1
Date: Mon, 17 Dec 2012 06:40:56 GMT
Keep-Alive: timeout=5, max=100
Proxy-Connection: Keep-Alive
Server: Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
Transfer-Encoding: chunked
Vary: accept-language,accept-charset


Method: POST
URL: http://192.168.0.29/mobile/read_mytask.php?cid=1
Body: 
cid=cid &
cus_name=c
& contact_no=n
& ticket_no=t
& taskdetail=td
Mona
  • 267
  • 1
  • 4
  • 9
  • have you got any error when running current code .if yes then plz share it for more help – ρяσѕρєя K Dec 17 '12 at 05:53
  • thanx ρяσѕρєя K for your response. you helped me last time for this issue. kindly guide me why it is not fetching data from database and display on android application. please have a look on logcat errors. – Mona Dec 17 '12 at 06:03
  • where is logcat i'm not able to see it – ρяσѕρєя K Dec 17 '12 at 06:06
  • in log you are getting timeout ConnectTimeoutException means your server is not responding so first make sure server is working fine or not. – ρяσѕρєя K Dec 17 '12 at 06:15
  • i have also tried your `http://198.168.0.29/mobile/update_mytask.php?cid=1` in browser but getting request time out – ρяσѕρєя K Dec 17 '12 at 06:22
  • does i upload my site on server??? what is the procedure for it?? – Mona Dec 17 '12 at 06:29
  • install [this](https://addons.mozilla.org/en-us/firefox/addon/restclient/) addon in firefox and make a post request from static parameters .put all parameters inside body section like parm1=a&parm2=d – ρяσѕρєя K Dec 17 '12 at 06:33
  • @ρяσѕρєя K you should be aware that the IP address used is an internal one for LANs (a private IP), and as such, it will never be routed on the internet, as per RFC 1918, so you will never be able to access her server from the Internet. – kaderud Dec 17 '12 at 06:50
  • @Mona I think your MySQL server is only listening for traffic on it's loopback interface (127.0.0.1), you need to check your "my.cnf" (or relevant MySQL config file) so that it also listens to traffic on it's LAN/WAN connected interface (usually eth0), or, the user you are using to connect to the MySQL server isn't connecting from an allowed ip. – kaderud Dec 17 '12 at 06:53
  • One way to easily verify if the MySQL server is listening for connections on interfaces connected to the same segment as your development machine, is to telnet to the MySQL server from your development machine, try: **telnet 198.168.0.29 3306** – kaderud Dec 17 '12 at 07:13
  • @chrkad: its not working:( – Mona Dec 17 '12 at 07:45
  • @Mona You need to say something more than "it's not working".. – kaderud Dec 17 '12 at 09:06
  • @chrkad: i think i have to upload my database on server then send HTTP request to get data from databse. is it right??? – Mona Dec 17 '12 at 09:15
  • @Mona I don't understand? Isn't your PHP script already running against your MySQL server? Then you shouldn't have to 'upload' anything. Just make sure your MySQL server is responding to network calls. Another way to verify if the MySQL server is listening for network calls is to logon your Linux box and issue the command **netstat -an|grep 3306** (you should get a row with LISTEN, beside the loopback interface (127.0.0.1) – kaderud Dec 17 '12 at 09:22
  • i am working on Windows 7. not in Linux. i have started xammp control where MySql & Php started automatically. don't knw why it's not listening to database. – Mona Dec 17 '12 at 09:51

2 Answers2

4

I'll give you an overview on how client-server data retrieval works in Android. You can tailor it to your specific requirements from there since your question indicates large foundational knowledge gaps to me.

You'll have to create REST API's (by writing server side code using a language like PHP, Ruby, Python etc) that your Android app can consume to obtain the data from your MySQL database.

There are other options that avoid REST API's as well. See:

How to get from a MySql server to an Android app?

See here for a beginners tutorial on REST:

http://net.tutsplus.com/tutorials/other/a-beginners-introduction-to-http-and-rest/

Once you have your API's written. You can query them using HTTP GET request and parse the result. If you are returning the data in JSON format, here's a tutorial on how to parse it:

http://www.vogella.com/articles/AndroidJSON/article.html

Now you can store the parsed data on your local database using the native android methods. See db tutorial here:

http://www.vogella.com/articles/AndroidSQLite/article.html

Once all this is over. You can do CRUD operations on your local database to read/manipulate your data and display them to the user using the XML based Android UI.

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
0
public class JsonHelper 
{
    static String response = null;
    String jsonRes = "";
    public JSONObject JObj=null;
    public final static int GET = 1;
    public final static int POST = 2;

//  public final static String fullUrl = "http://10.0.2.2/fluentcmsonline/";

public JsonHelper()
    {

    }

    public JSONObject MakeJsonCall(String Url,int method)
    {
        return this.MakeJsonCall(Url, method, null);
    }

    public JSONObject MakeJsonCall(String Url,int method, List<NameValuePair> params)
    {
        try
        {
            DefaultHttpClient HttpClient = new DefaultHttpClient();
            HttpEntity httpentity = null;
            HttpResponse httprespo = null;
            if(method==POST)
            {
                 HttpPost httppost = new HttpPost(fullUrl+Url);
                //HttpPost httppost = new HttpPost(Url);
                if(params != null)
                {
                    httppost.setEntity(new UrlEncodedFormEntity(params));
                }

                httprespo = HttpClient.execute(httppost);
            }
            else
            {
                if(params != null)
                {
                    String paramString = URLEncodedUtils.format(params, HTTP.UTF_8);

                    Url += "?"+paramString;     
                }

                 HttpGet httpget = new HttpGet(fullUrl+Url);
                //HttpGet httpget = new HttpGet(Url);

                httprespo = HttpClient.execute(httpget);
            }
            httpentity = httprespo.getEntity();

            if(httpentity != null)
            {
                InputStream is = httpentity.getContent();
                BufferedReader buffer = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);

                StringBuilder Sb = new StringBuilder();
                String Line = null;

                while((Line = buffer.readLine())!=null)
                {
                    Sb.append(Line+"\n");
                }
                is.close();
                jsonRes = Sb.toString();

            }

        }
         catch (UnsupportedEncodingException e) {
                e.toString();
        } catch (ClientProtocolException e) {
            e.toString();
        } catch (IOException e) {
                e.toString();
        }

        try
        {
            JObj = new JSONObject(jsonRes);
            Log.e("Data", jsonRes);
        }
        catch(JSONException e)
        {
            e.printStackTrace();
        }

        return JObj;
    }

}