繁体   English   中英

使用HTTP发布方法将数据从android应用发送到PHP页面

[英]Sending data from android app to a PHP page using HTTP post method

我是android开发的新手。 我正在尝试一个基本的应用程序。 我的要求是我需要使用POST方法将数据从android应用程序中的文本框发送到网络上托管的PHP页面。 我浏览了许多站点,以获取有关此方法的教程,但没有找到合适的方法。 谁能提供给我一个示例代码,以便使用post方法将数据从android应用程序中的一个文本框发送到PHP页面吗? 先感谢您。

像这样:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText input = (EditText)findViewById(R.id.yourinput);
    input.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String text = input.getText().toString();
            new UploadTask().execute(text);
        }
    });
}

private class UploadTask extends AsyncTask<String, Integer, String> {

    private ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(MainActivity.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(
                "http://yourwebsite.com/commit.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs
                    .add(new BasicNameValuePair("username", params[0]));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            if (response != null) {
                InputStream in = response.getEntity().getContent();
                String responseContent = inputStreamToString(in);

                return responseContent;
            }
        } 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);
    }

    private String inputStreamToString(InputStream is) throws IOException {
        String line = "";
        StringBuilder total = new StringBuilder();

        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        // Read response until the end
        while ((line = rd.readLine()) != null) { 
            total.append(line); 
        }

        // Return full string
        return total.toString();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM