我正在尝试从Android应用程序发送数据以显示在php页面中。根据我放置的日志,数据发送并具有正确的值,但在php页面上,它显示一个空数组:
Array()
这是我的php文件:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "Post made";
}
$JSON_Received = $_POST["JSON"];
$JSON_test = $_POST["TEST"];
$json = file_get_contents('php://input');
$obj = json_decode($JSON_test);
$obj1 = json_decode($JSON_Received);
$obj2 = json_decode($json);
echo $obj1;
echo $obj;
echo $json;
echo $JSON_test;
echo $JSON_Received;
print_r($_POST);
?>
Java代码:
public class confirmFragmentTab extends Fragment implements OnItemClickListener,
OnItemLongClickListener {
public static final String ARG_ITEM_ID = "sales_list";
ListView saleListView;
ArrayList<SalesReciepts> salesReciept;
SalesListAdapter salesListAdapter;
ProductsDbHelper db, dp;
Activity activity;
Context context;
Button push;
private String POST_PRODUCTS = "http://bi.test.com/tests/products_api/post_products.php";
InputStream is = null;
private GetEmpTask task;
protected FragmentActivity mActivity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
context = getActivity();
db = new ProductsDbHelper(activity);
}
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = (FragmentActivity) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.confirm_layout, container, false);
findViewsById(rootView);
task = new GetEmpTask(activity);
task.execute((Void) null);
saleListView.setOnItemClickListener(this);
saleListView.setOnItemLongClickListener(this);
GlobalApp.data().context = getActivity();
push.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//postList();
new pushInvoice().execute();
}
});
return rootView;
}
private void findViewsById(View view) {
saleListView = (ListView) view.findViewById(R.id.list_sale);
push = (Button) view.findViewById(R.id.sendRecords);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onItemClick(AdapterView<?> list, View arg1, int position,
long arg3) {
SalesReciepts sale = (SalesReciepts) list.getItemAtPosition(position);
if (sale != null) {
Bundle arguments = new Bundle();
arguments.putParcelable("selectedSale", sale);
CustomEditDialog customDialogFragment = new CustomEditDialog();
customDialogFragment.setArguments(arguments);
customDialogFragment.show(getFragmentManager(),
CustomEditDialog.ARG_ITEM_ID);
}
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long arg3) {
SalesReciepts sale = (SalesReciepts) parent.getItemAtPosition(position);
// Use AsyncTask to delete from database
db.delete(sale);
salesListAdapter.remove(sale);
return true;
}
public class GetEmpTask extends AsyncTask<Void, Void, ArrayList<SalesReciepts>> {
private final WeakReference<Activity> activityWeakRef;
public GetEmpTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
@Override
protected ArrayList<SalesReciepts> doInBackground(Void... arg0) {
ArrayList<SalesReciepts> saleList = db.getCurrentSalesRecords();
return saleList;
}
@Override
protected void onPostExecute(ArrayList<SalesReciepts> sList) {
if (activityWeakRef.get() != null
&& !activityWeakRef.get().isFinishing()) {
Log.d("sales", sList.toString());
salesReciept = sList;
if (sList != null) {
if (sList.size() != 0) {
salesListAdapter = new SalesListAdapter(activity,
sList);
saleListView.setAdapter(salesListAdapter);
} else {
Toast.makeText(getActivity(), "No Sales Records",
Toast.LENGTH_LONG).show();
}
}
}
}
}
private class pushInvoice extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
//ArrayList<RecieptHeader> invoiceList = db.getInvoiceHeader();
ArrayList<SalesReciepts> entryList = db.getSalesRecords();
List<NameValuePair> postVars = new ArrayList<NameValuePair>();
postVars.add(new BasicNameValuePair("JSON", String.valueOf(entryList)));
postVars.add(new BasicNameValuePair("TEST", "HELLO"));
ServiceHandler jsonParser = new ServiceHandler();
try {
HttpClient httpClient = new DefaultHttpClient();
// Setup a http post method for passing the URL in case
// the database is offline and the ip address in case of a localhost database
HttpPost httpPost = new HttpPost(POST_PRODUCTS);
//HttpPost httpPost = new HttpPost("http://192.168.1.33/connect_to_new_table.php");
// Pass on the names of the values inside the post
StringEntity entity = new StringEntity(postVars.toString());
httpPost.setEntity(entity);
httpPost.setEntity(new UrlEncodedFormEntity(postVars));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpClient.execute(httpPost,
responseHandler);
is = entity.getContent();
if (response != null)
Log.e("Sent", postVars.get(1).getValue().toString());
Log.e("Response", response);
} catch (Exception e) {
Log.e("Error", e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
}
更新: 记录已发送的值。 postVars是要发送的值的名称值列表
08-19 10:19:22.194 1708-1725/com.example.Prototype E/Sent﹕ HELLO
08-19 10:19:22.194 1708-1725/com.example.Prototype E/Response﹕ Post madeJSON=%5BSalesReciepts+%5Bid%3D0%2C+product_description%3DBell+Lager+500ml+RET+25X01+LONGNECK%2C+qty%3D23%2C+unit%3D3658%2C+total%3D84134.0%5D%5D&TEST=HELLOHELLO[SalesReciepts [id=0, product_description=Bell Lager 500ml RET 25X01 LONGNECK, qty=23, unit=3658, total=84134.0]]Array
(
[JSON] => [SalesReciepts [id=0, product_description=Bell Lager 500ml RET 25X01 LONGNECK, qty=23, unit=3658, total=84134.0]]
[TEST] => HELLO
)
答案 0 :(得分:0)
几周前我做了类似的事情,我得到了这个结构的JSON数据:
$json = json_decode(file_get_contents('php://input'), true);
然后用json ['nameVar']得到值。
如果项目可以帮助您,您可以在Github找到我的项目。