1

i have problem on asychronous http post, I've managed to send data from EditText to the server if run using the emulator. but fails when using a real device. how to be able to send data from android studio to the server(xampp) if run using tge real device?

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText value;
    private Button btn;
    private ProgressBar pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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 void onClick(View v) {
        if (value.getText().toString().length()<1){
            Toast.makeText(getApplicationContext(),"Isi sesuatu Dong!",Toast.LENGTH_LONG).show();
        }else{
            pb.setVisibility(View.VISIBLE);

        }
    }

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

        @Override
        protected Double doInBackground(String... params) {
           postData(params[0]);
            return null;
        }


        protected void onPostExecute(Double hasil) {
            pb.setVisibility(View.GONE);
            Toast.makeText(getApplicationContext(),"Berhasil dikirim",Toast.LENGTH_LONG).show();
        }

        protected void onProgressUpdate(Integer... progress) {
            pb.setProgress(progress[0]);
        }

        public void postData(String nilaiYangDikirim){
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/http/index.php");

            try {
                List<NameValuePair>  nilai = new ArrayList<NameValuePair>();
                nilai.add(new BasicNameValuePair("myHttpData", nilaiYangDikirim));
                httpPost.setEntity(new UrlEncodedFormEntity(nilai));
                HttpResponse response = httpClient.execute(httpPost);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }
    }
}
Anggit Prayogo
  • 103
  • 1
  • 11

1 Answers1

1

If you are going to test the code by using real device then you have to note down the IP address of the system.

Goto CMD->type ipconfig /all(windows) and in ubuntu type ip [options]

Then note the ipaddress of the wireless lan adapter(that may start with the 192.168.* .*)

Then Goto xampp folder and search for httpd-xampp.conf

Type Allow from 192.168. *. * at the end of the file

Create Adhoc network or wifi P2P network in system

Connect to the P2p network in mobile

Finally change the code like this

 HttpPost httpPost = new HttpPost("http://192.168.*.*/http/index.php");
akhil Rao
  • 1,169
  • 7
  • 17