In my android application, I am querying a mysql database for a specific food id.

Example:

Food A - (food_id 5)

Food A is part of three food groups, each with their own unique ID.

Example:

Meal A - (meal_id = 3), 
Meal B - (meal_id = 8),
Meal C - (meal_id = 20)

I am currently connecting to the mysql database via php, and can successfully query for the food_id in question and receive the JSON response into my android application.

I am stuck how to then pass it back through php/JSON to create three mysql rows (one for each meal returned) which will include some extra information added onto it in some new fields. Would I best be served by passing an array from android to my php service? How would this look?

有帮助吗?

解决方案

Just use an HttpClient to make a request back to the PHP application endpoint where you want to handle the incoming data, POSTing the data you want to work with on the server. You can post in whatever format suits you - query string, JSON, etc.

其他提示

You can send data to your PHP files using HTTPClient

Here's a quick example:

    url = "http://10.0.218.211/post.php"; 

HttpClient httpclient = new DefaultHttpClient();  
HttpPost post = new HttpPost(url);  

try {  

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
        nameValuePairs.add(new BasicNameValuePair("meal1", "important message 1"));  
        nameValuePairs.add(new BasicNameValuePair("meal2", "important message 2"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

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

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

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

post.php

<?php

$meal1 = $_POST['meal1'];
$meal2 = $_POST['meal2'];

?>

It sounds like you need to design some sort of API. The typical options are implementation of SOAP or REST. As SOAP has a lot of overhead, you could alternatively create a REST interface to your system. I'm just guessing, but an API call like this might make sense:

POST /user/meal

With a form that would include form fields for: userId - the id of the user to create the meal plan for meals - a comma delimitted list of the meals, or alternatively an array element

Your PHP script would simply need to get the POST params from $_POST, insert the appropriate rows, and return a valid HTTP status code.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top