Iam是android中的新功能,正在开发反馈型的android应用程序,其中我使用实体类在本地存储所有数据,现在我需要使用php url将此数据发送到服务器,所以请您能帮助我如何将我的数据发布到服务器上。
您可以使用以下代码
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();
}
}
并在Appstate类中创建Appstate类,以创建构造函数并定义要在数据库中传递的所有变量字段...
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=" ";
}
}
在数据库已归档的情况下更改appstate字段
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();
}
}
注意:sdk 23不再支持DefaultHttpClient,因此建议将此目标sdk 21与此代码一起使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句