Android + PHP: Sending a GET request to PHP server

若如初见. 提交于 2019-12-03 21:53:15

Your code could be first of all simplified, i suggest you to use this android library: http://loopj.com/android-async-http/ which allows you to make every kind of HTTP requests.

After you downloaded it you should obviously import it in your project.

Code with Android Asynchronous Http Client implemented

public class Final extends Activity {

AsyncHttpClient client = new AsyncHttpClient();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_final);


    Button btnSend = (Button)findViewById(R.id.BtnSend);



    btnSend.setOnClickListener(new OnClickListener(){

    public void onClick(View arg0){

            TestReceiver();


        }
    });



void TestReceiver()
{

        //ALERT MESSAGE
        Toast.makeText(getBaseContext(),"Please wait, connecting to server.",Toast.LENGTH_LONG).show();
        String Value="Kevin";

        String url = "http://echogame24.comli.com/Receiver.php?action="+Value;


        client.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                Toast.makeText(getBaseContext(),"Request made successfully.",Toast.LENGTH_LONG).show();
            }
        });

    }
}


}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!