android HTTP发布php脚本问题

时间:2016-05-12 16:35:50

标签: java php android http

从Android到web的android http发布数据我有制作应用程序,它将数据从android发送到web android app发送数据perfact但我不知道如何在php中编码从android获取数据。 我正在使用xampp服务器的PHP脚本。这是应用程序代码。

    public class Main extends Activity implements OnClickListener{

    private EditText value;
    private Button btn;
    private ProgressBar pb;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
        value=(EditText)findViewById(R.id.editText1);
        btn=(Button)findViewById(R.id.button1);
        pb=(ProgressBar)findViewById(R.id.progressBar1);
        pb.setVisibility(View.GONE);
        btn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
            if(value.getText().toString().length()<1){

                // out of range
                Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
            }else{
                pb.setVisibility(View.VISIBLE);
                new MyAsyncTask().execute(value.getText().toString());      
            }


    } 

    private class MyAsyncTask extends AsyncTask<String, Integer, Double>{

        @Override
        protected Double doInBackground(String... params) {
            // TODO Auto-generated method stub
            postData(params[0]);
            return null;
        }

        protected void onPostExecute(Double result){
            pb.setVisibility(View.GONE);
            Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
        }
        protected void onProgressUpdate(Integer... progress){
            pb.setProgress(progress[0]);
        }

        public void postData(String valueIWantToSend) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://somewebsite.com/receiver.php");

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }

    }
}

这是我的xml文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Enter Something Below:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:ems="10"
        android:hint=""
        >

        <requestFocus />
    </EditText>

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/progressBar1"
        android:layout_marginTop="24dp"
        android:text="Submit" />

</RelativeLayout>

这是php脚本代码reciver.php

<?php
 // receive data from app's http request
 $data=$_POST["myHttpData"];
 // write data from my android app to a text file
 file_put_contents('myTextFile.txt',$data);
?>

请告诉我如何用正确的代码制作php脚本

它在php中显示我这样的错误。 enter image description here

1 个答案:

答案 0 :(得分:0)

按照步骤

  1. 创建名为test.php的文件并将该PHP代码复制到该文件中。
  2. 将该文件复制到xamp服务器根文件夹(位于WWW目录下)
  3. 如果是,请确保您的手机和计算机连接到同一个WiFi路由器,然后复制系统的IP地址。
  4. 复制的IP地址将是您的服务器IP。您可能需要搜索端口号。
  5. 经过以上4个步骤后,您最终会得到一些像这样的URL http://IP adreas:端口号/ test.php 示例http://192.168.1.1:8080/test.php
  6. 运行您的xamp服务器。
  7. 在您的Android http请求中设置此网址。
  8. 在Google中查找错误。
相关问题