How to get http to receive a request in Android

I am new to android.So, I can tell me how to make an http request e.g.

GET /photos?size=original&file=vacation.jpg HTTP/1.1 Host: photos.example.net:80 Authorization: OAuth realm="http://photos.example.net/photos", oauth_consumer_key="dpf43f3p2l4k3l03", oauth_token="nnch734d00sl2jdk", oauth_nonce="kllo9940pd9333jh", oauth_timestamp="1191242096", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D" 

in android (java)?

+7
source share
1 answer

You want to get to know InputStreams and OutputStreams in Android if you did it in regular java, and then, in essence, it is the same thing. You need to open the connection with the request property as "GET", then you write your parameters to the output stream and read the response through the input stream. You can see this in my code below:

  try { URL url = null; String response = null; String parameters = "param1=value1&param2=value2"; url = new URL("http://www.somedomain.com/sendGetData.php"); //create the connection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //set the request method to GET connection.setRequestMethod("GET"); //get the output stream from the connection you created request = new OutputStreamWriter(connection.getOutputStream()); //write your data to the ouputstream request.write(parameters); request.flush(); request.close(); String line = ""; //create your inputsream InputStreamReader isr = new InputStreamReader( connection.getInputStream()); //read in the data from input stream, this can be done a variety of ways BufferedReader reader = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } //get the string version of the response data response = sb.toString(); //do what you want with the data now //always remember to close your input and output streams isr.close(); reader.close(); } catch (IOException e) { Log.e("HTTP GET:", e.toString()); } 
+14
source

All Articles