简体   繁体   中英

android HTTP post php script issue

android http post data from android to web i have make app which send data from android to web android app send data perfact but i dont know how to code in php to recived data from android. i am using xampp server for php script. this is mt app code.

    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
            }
        }

    }
}

this my xml file

    <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>

this is php script code 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);
?>

please tell how can i make php script with proper code

it show me error like this in php. 在此输入图像描述

Follow the steps

  1. Create file named test.php and copy your php code in that file.
  2. Copy that file in xamp server root folder ( in side WWW directory)
  3. Make sure your mobile and computer connected to same WiFi router if so then copy the IP address of your system.
  4. Copied IP address will your server IP . You may need to do search port number.
  5. After above 4 steps you end up with URL some thing like this http://IP adreas : port number/test.php Example http://192.168.1.1:8080/test.php
  6. Run your xamp server.
  7. Set this url in your android http request.
  8. Look for errors n Google .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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