Hello I'm new to this too but I've managed to find a way to send and
receive data using a php script.

To send I have used this method

public void Post() throws ClientProtocolException, IOException
        {
                /** Connect with the PHP script */
                String address = "http://www.yourwebsite.com/script.php";;
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(address);
                /** Set Post data - use an array of name value pairs */
                ArrayList<NameValuePair> entry = new ArrayList<NameValuePair>();
                /** Add these values */
                entry.add(new BasicNameValuePair("key", "value"));
                entry.add(new BasicNameValuePair(" Id_Famille", "Johnson"));
                post.setEntity(new UrlEncodedFormEntity(entry));

                /** Execute post request */
                HttpResponse response = client.execute(post);
        }

To receive data I have used this class...

public String get()
        {
                String entityContents="";
                InputStream is = null;
                try
                {
                        // Connect to PHP script
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpGet httpget = new 
HttpGet("http://www.yourwebsite.com/
script.php");
                        HttpResponse response = httpclient.execute(httpget);
                        HttpEntity entity = response.getEntity();

                        byte buffer[] = new byte[1024] ;
                        is = entity.getContent() ;
                        int numBytes = is.read(buffer) ;
                        is.close();

                        entityContents = new String(buffer,0,numBytes) ;
                        Log.d("xxx",entityContents);

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

I am at the stage where I need to convert the returned entityContents
value into a string without all of the json stuff around it.
The output looks like this...

[{"key":"value"}]

and all I want is the value.
If anyone knows how - that would be useful!

On Aug 18, 12:51 pm, khalfaoui ameni <[email protected]>
wrote:
> thanks Sarwar Erfan for your answer,but can you tell me how i write
> the script and where i put it ??
> i tried to write a php script
>
> <?php
> $myServer = "98.130.0.21";
> $myUser = "********";
> $myPass = "******";
> $myDB = "######";
>
> //connection to the database
> $dbhandle = mssql_connect($myServer, $myUser, $myPass)
>   or die("Couldn't connect to SQL Server on $myServer");
>
> //select a database to work with
> $selected = mssql_select_db($myDB, $dbhandle)
>   or die("Couldn't open database $myDB");
>
> //declare the SQL statement that will query the database
> $query = "SELECT Id_Famille, Id_filiale, Designation ";
> $query .= "FROM T_Article ";
>
> while($e=mssql_fetch_assoc($query))
>         $output[]=$e;
>
> print(json_encode($output));
>
> //close the connection
> mssql_close($dbhandle);
> ?>
> and i put my scipt in my pc c:/
> in my application i write this :
>  String result = "";
>
>       ArrayList<NameValuePair> nameValuePairs = new
> ArrayList<NameValuePair>();
>
>       InputStream is = null;
>       //http post
>       try{
>               HttpClient httpclient = new DefaultHttpClient();
>               HttpPost httppost = new HttpPost("http://10.0.2.2/
> getAllArticle.php");
>
>               HttpResponse response = httpclient.execute(httppost);
>               HttpEntity entity = response.getEntity();
>               is = entity.getContent();
>       }catch(Exception e){
>               Log.e("log_tag", "Error in http connection
> "+e.toString());
>       }
>       //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");
>               }
>               is.close();
>
>               result=sb.toString();
>       }catch(Exception e){
>               Log.e("log_tag", "Error converting result
> "+e.toString());
>       }
>
>       //parse json data
>       try{
>               JSONArray jArray = new JSONArray(result);
>               for(int i=0;i<jArray.length();i++){
>                       JSONObject json_data = jArray.getJSONObject(i);
>                       Log.i("log_tag","id_famille:
> "+json_data.getString("Id_Famille")+
>                               ", id_filiale:
> "+json_data.getString("Id_filiale")+
>                               ", designation:
> "+json_data.getString("Designation ")
>
>                       );
>               }
>       }
>       catch(JSONException e){
>               Log.e("log_tag", "Error parsing data "+e.toString());
>       }
>     }
> but this code don't work
> 08-18 11:09:51.281: ERROR/log_tag(299): Error in http connection
> org.apache.http.NoHttpResponseException: The target server failed to
> respond
> 08-18 11:09:51.281: ERROR/log_tag(299): Error converting result
> java.lang.NullPointerException
> 08-18 11:09:51.311: ERROR/log_tag(299): Error parsing data
> org.json.JSONException: End of input at character 0 of
>
> could you please help me to fix it or did you have another way to do
> this?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to