Iam new in android, am developing feedback kind of android application where i am storing all data locally using entity class,now i need to send this data to server using php url, so can you please help me how can i POST my data on server.
Asked
Active
Viewed 1.2k times
1
-
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ – Shijil Dec 11 '13 at 07:03
3 Answers
3
you can use below code
public class UploadData extends AsyncTask<String, Void, AppState> {
private static int[] number=new int[100];
private static int count=0;
String edit_id2;
private int numItems=0;
@Override
protected void onPostExecute(AppState result) {
UserdataList data= new UserdataList() ;
};
@Override
protected AppState doInBackground(String... params) {
List<NameValuePair> nameValuePairs;
UserdataList data= new UserdataList() ;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://your serverlink/foldername/filename.php");
nameValuePairs = new ArrayList<NameValuePair>(50);
numItems = data.getDataList().size();
for(int i=0; i<numItems ;i++)
{
if(data.getDataList().get(i).transfered){
continue;
} else
if(!data.getDataList().get(i).transfered)
{
nameValuePairs.add(new BasicNameValuePair("address", data.getDataList().get(i).address));
nameValuePairs.add(new BasicNameValuePair("store", "Kosmo"));
nameValuePairs.add(new BasicNameValuePair("appearance", data.getDataList().get(i).appearance));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_16));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
nameValuePairs);
httppost.setEntity(formEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
int status = response.getStatusLine().getStatusCode();
if(status == 200){
System.out.println("ResponseCode of record: "+ i + " is " + status);
data.getDataList().get(i).transfered=true;
System.out.println("Transfered");
nameValuePairs.clear();
} else {
System.out.println(" NOT Transfered");
data.getDataList().get(i).transfered=false;
}
}
}
data.reInitializeList();
data.getDataList().clear();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
return data.getUserdata();
}
}
and create Appstate Class inside Appstate class create constructor and define all the variable field which you want to pass in database...
public class AppState implements Serializable{
public String appointment="", address="", mannerism="", consultation="", treatment="",advice="", appearance="", billing="", waiting="",experience="",suggestions="";
public String pname="", formatted,phone=" ";
AppState userdata;
boolean transfered;
AppState(){
this.address=" ";
this.appearance="";
this.suggestions="";
this.appointment=" ";
this.mannerism=" ";
this.experience="";
this.consultation=" ";
this.treatment=" ";
this.advice=" ";
this.billing=" ";
this.waiting=" ";
this.pname=" ";
this.phone =" ";
this.formatted=" ";
}
}
Change appstate field with your database filed
public class UserdataList {
private static List<AppState> userDataList;
private static AppState userdata;
public AppState getUserdata() {
if (null == userdata) {
userdata = new AppState();
}
return userdata;
}
public List<AppState> getDataList() {
if (null == userDataList) {
userDataList = new ArrayList<AppState>();
}
return userDataList;
}
public void addDataToList(AppState data) {
if (null == userDataList) {
userDataList = new ArrayList<AppState>();
}
userDataList.add(data);
userdata = null;
}
public void reInitializeList() {
userDataList.clear();
}
public void sendData() {
new UploadData().execute(" ");
userdata = null;
}
public UserdataList() {
getUserdata();
}
}
Note: DefaultHttpClient is no longer supported by sdk 23, so it is advisable to use target sdk 21 with this code.

xerex09
- 166
- 14

InnocentKiller
- 5,234
- 7
- 36
- 84
-
Iam very new to android can you please tell me how to create AppState class and how to create a construtor.. – Raghu Kaligipula Dec 11 '13 at 07:42
-
-
1@RaghuKaligipula I have updated my answer... Please check that and still if you have any query fill free to ask me. Happy to help you. – InnocentKiller Dec 11 '13 at 08:27
-
thanks for the code, but at public class UserdataList its getting me this error "The public type UserdataList must be defined in its own file" where i have to define it? – Raghu Kaligipula Dec 11 '13 at 09:18
-
Create it in new java file. not in Appstate class. create new UserdataList.java file in your project. see my edited answer for reference. – InnocentKiller Dec 11 '13 at 09:31
-
hello, i need to send data from app to php server, it includes boolean,comments and text. i am saving all data in entity object, how can i make use of them? can you please help me out for this.? – Raghu Kaligipula Dec 12 '13 at 07:32
-
@RaghuKaligipula, above code does the same, it will send app data to your database with PHP file, which you need to create on your server. – InnocentKiller Dec 12 '13 at 08:10
-
Thanks a lot, i will try the same, inbetween can you help me out to finish the app – Raghu Kaligipula Dec 12 '13 at 10:48
-
-
i need to send all images and data to server, by only clicking "Submit Now" in SignOut Class Activity.. Signout Activity consists of two buttons "Submit Now"(online) and "Submit Later"(offline) In case of "Submit Later"(offline) it should not upload to server, but it should locally saved(that i have done) and again he has to login as "online" and he should able to upload the "Submit Later" data using "upload button" in Sync Activiy which i have created. – Raghu Kaligipula Dec 12 '13 at 11:29
0
you can use httppost class to help you in your case you can refer to the following URL to help you
http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

Mohammad Gabr
- 221
- 2
- 5
0
Http-Request library would help you a lot and make your programming life much more easier ;) read short guide and use it :)

Mieszko
- 316
- 2
- 8