Answer the question
In order to leave comments, you need to log in
How to send a request from android to php server to output one line?
There is one php server where it says: Hello World! Found one source, but did not understand
SEND HTTP GET REQUEST
To send HTTP GET request follow the steps.
1. Create an object of HttpClient
HttpClient client = new DefaultHttpClient();
2. Create an object of HttpGet
HttpGet request = new HttpGet("http://www.example.com");
3. Finally make HTTP request
HttpResponse response;
try {
response = client.execute(request);
Log.d("Response of GET request", response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ANDROID HTTP GET EXAMPLE
package com.example.httprequestexample;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class HTTPGETActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
makeGetRequest();
}
private void makeGetRequest() {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.example.com");
// replace with your url
HttpResponse response;
try {
response = client.execute(request);
Log.d("Response of GET request", response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Answer the question
In order to leave comments, you need to log in
What is incomprehensible?
a simple example of sending a GET request with comments
ОТПРАВКА HTTP GET ЗАПРОСА
ДЛЯ ОТПРАВКИ GET ЗАПРОСА СЛЕДУЙТЕ ЗА ДАННЫМИ ШАГАМИ:
1. Создаем объект HttpClient
HttpClient client = new DefaultHttpClient();
2. Создаем объект HttpGet
HttpGet request = new HttpGet("http://www.example.com");
3. И наконец делаем HTTP запрос
HttpResponse response;
try {
response = client.execute(request);
Log.d("Ответ GET запроса", response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question