gpt4 book ai didi

java - android中如何将数据发布到服务器?

转载 作者:行者123 更新时间:2023-12-01 11:11:13 25 4
gpt4 key购买 nike

嗨,我想将数据发布到 php 服务器,我有一个像

的参数
parameters:upid,product_id,pname,pquantity,price,pickup_date,pickup_time,name,email,mobile,address,location,pincode,total_amount

我在 Activity 类中调用 asyncTask

PlaceOrderTask placeOrderTask = new PlaceOrderTask(YourBasket.this,upid,"2", iconName,"200", "250.00", dateFormat,timeFormat, name,email,mobileNo,"Hyderbad",location, "500032", totalAmount);
placeOrderTask.execute();

在我的 AsyncTask 中我会这样调用

          public class PlaceOrderTask extends AsyncTask 
{
String upid, product_id, productName, productQuantity, pprice, date, time,
name, email, mobileNo, location, address, pincode, totalAmount;
private ProgressDialog pd = null;
Context context;
public PlaceOrderTask(Context context,String upid, String product_id, String productName,
String productQuantity, String pprice, String date, String time,
String name, String email, String mobileNo, String location,
String address, String pincode, String totalAmount)
{
this.context = context;
this.upid = upid;
this.product_id = product_id;
this.productName = productName;
this.productQuantity = productQuantity;
this.pprice = pprice;
this.date = date;
this.time = time;
this.name = name;
this.email = email;
this.mobileNo = mobileNo;
this.location = location;
this.address =address;
this.pincode =pincode;
this.totalAmount = totalAmount;
}

@Override
protected Object doInBackground(Object... params)
{

return null;
}

@Override
protected void onPreExecute()
{
pd = new ProgressDialog(context);
pd.setMessage("Please Wait.....");

super.onPreExecute();
}

@Override
protected void onPostExecute(Object result) {

pd.dismiss();
super.onPostExecute(result);
}
}

我们在 doInBackground 和 onPostExcute 中调用的内容

and Here My url is like this
http://safewash.docni.in/iosapp/order.php

这是我的问题,任何人都可以建议我如何做到这一点

最佳答案

Post 类型请求的异步任务:

import java.io.IOException;
import java.net.UnknownHostException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;

public class WebPostAsyncTask extends AsyncTask<String, String, String>
{
private String url = null;
private int id;
private OnResponseListener listener;
private String jsonString;
private String header = null;
private String headerKey;

public WebPostAsyncTask(String url, int id, OnResponseListener listener, String jsonString)
{
this.url = url;
this.id = id;
this.listener = listener;
this.jsonString = jsonString;
}

public WebPostAsyncTask(String url, int id, OnResponseListener listener, String jsonString, String header, String headerKey)
{
this.url = url;
this.id = id;
this.listener = listener;
this.jsonString = jsonString;
this.header = header;
this.headerKey = headerKey;
}

@Override
protected void onPreExecute()
{
super.onPreExecute();
}

@Override
protected String doInBackground(String... params)
{
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json; charset=utf-8");

if (header != null)
{
httpPost.addHeader(headerKey, header);
}

httpPost.setEntity(new StringEntity(jsonString, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String response = EntityUtils.toString(httpEntity);
return response;
}
catch (ClientProtocolException e)
{
}
catch (UnknownHostException e)
{
}
catch (IOException e)
{
}
catch (Exception e)
{
}
return null;
}

@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
if (listener != null)
{
listener.onResponse(id, result);
}
}

public interface OnResponseListener
{
public void onResponse(int id, String response);
}

}

要在您的 Activity 中使用它:

public class TrialActivity extends Activity
{
private static final int URL_ID = 1;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
JSONObject jObject = new JSONObject();
try
{
jObject.put("data1", "trial_data1");
jObject.put("data2", "trial_data1");
}
catch (JSONException e)
{
}
new WebPostAsyncTask("YOUR_URL_HERE", URL_ID, listener, jObject.toString()).execute();
}

private OnResponseListener listener = new OnResponseListener()
{
@Override
public void onResponse(int id, String response)
{

switch (id)
{
case URL_ID:
// You will get your service response here
}
}
};
}

代码非常简单,如果您仍然遇到任何问题,请给我评论。

关于java - android中如何将数据发布到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com