使用POST从Android发送数据到PHP

时间:2014-02-06 15:41:46

标签: php android post

我无法将数据从我的应用程序发送到我的localhost服务器上的PHP脚本。我可以从PHP脚本中获取信息,但似乎无法向其发布信息。我正在尝试将ID发送到该文件以用于查询数据库。当我加载PHP文件时,我只是在从应用程序发送数据后得到一个空白页面,该应用程序在模拟器上运行。我没有在logcat或加载PHP文件时收到任何错误。谢谢你的帮助。

下面是我的java代码:

public class EventInformation extends Activity
{
     private String url = "http://10.0.2.2/Web-Coding/android_get_event.php";
     TextView ev_title, ev_desc;
     Intent intent;
     public String the_title, the_desc;
     public HttpClient httpclient;
     public HttpPost httppost;
     StringBuilder sb1;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eventinformation_layout);

         ev_title = (TextView)findViewById(R.id.event_title);
         ev_desc = (TextView)findViewById(R.id.event_description);

        String text = "Hello!";
        new UploadTask().execute(text);
    }

        private class UploadTask extends AsyncTask<String, Integer, String> 
        {
            private ProgressDialog progressDialog;
            @Override
            protected void onPreExecute() 
            {
                progressDialog = new ProgressDialog(EventInformation.this);
                progressDialog.setMessage("Uploading...");
                progressDialog.setCancelable(false);
                progressDialog.setIndeterminate(true);
                progressDialog.show();
                super.onPreExecute();
            }

            @Override
            protected String doInBackground(String... params) 
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("id", "23"));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity resEntity = response.getEntity();

                    if (resEntity != null)
                    {
                        String responseStr = EntityUtils.toString(resEntity).trim();
                        Log.v("TAG", "Response: " +  responseStr);
                    }

                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            return null;
        }

        @Override
        protected void onPostExecute(String result) 
        {
            if (progressDialog != null) {
                progressDialog.dismiss();
            }
            // process the result
            super.onPostExecute(result);
        }
    }
}

以下是获取和显示发布数据的PHP代码:

<?php 

    if($_POST)
    {
        print_r($_POST);
    }

?>

1 个答案:

答案 0 :(得分:0)

在Log.v()

之后添加以下行
return responseStr;

这会将响应转发给onPostExecute()