Reputation: 157
I want to order some items using android application.I have one xml file, in this xml file some edit texts are there,entered some text what items i want to purchase from client.Then click on submit button. Then these items details are send to the client database. How can i send these purchase details to the client's database?
Upvotes: 3
Views: 4386
Reputation: 701
follow this tutorial(best one) and i have added some concrete code
link :http://www.androidhive.info/category/mysql/page/2/
link :http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
// url to create new product // you have to create php file to connect with server and below url is just example
private static String url_create_product = "http://10.0.2.2/html/atm_database`/customers.php";
// Edit Text
edittext= (EditText) findViewById(R.id.input1);
edittext= (EditText) findViewById(R.id.input2);
// Create button
Button sub = (Button) findViewById(R.id.submit);
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// write code here to get data from edittext
// creating new product in background thread
new CreateNewProduct().execute();
}
});
}
/** * Background Async Task to Create new product * */
class CreateNewProduct extends AsyncTask<String, String,String> {
/**
* Before starting background thread Show Progress Dialog
* */
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Ac1.this);
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Log in
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();//namevaluepair is a class in apachae for setting name-val parameter
params.add(new BasicNameValuePair("id", data1);//BasicNameValuePair is a subclass of NameValuePair is data you have to server
params.add(new BasicNameValuePair("pass",data2));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
// Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
String cust_name=json.getString("name");
String sessid=json.getString("id");
if (success == 1) {
// successfully created product
//write code to do things getting response from server
} else {
Log.w("else", "wrong id");
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(Ac1.this, "Wrong ID or Password!!", Toast.LENGTH_LONG).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
// class to connect server
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {//JSONObject used to convert one data type to other
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));//UrlEncodedFormEntity-->converts string to encoded string to represent in post method
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();//getContent is used to to extract data frm connection between app and php
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {//ClientProtocolException-->error in http protocol
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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();
json = sb.toString();
Log.i("StringBuilder...", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parserrr", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Upvotes: 2