2012-06-26 16 views
7

Soy nuevo en android.So puedo cualquiera sho me cómo hacer una petición HTTP GET comocómo hacer la solicitud HTTP GET en Android

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" 

en Android (Java)?

+4

creo que esto le dará responder: http://stackoverflow.com/questions/3505930/make-in-http-request-with-android – DRAX

+0

Pero cuando debo poner todos los parámetros como oauth_consumer_key, etc ... –

+0

esto debería comenzar con juramento -> http://stackoverflow.com/questions/2150801/implementing-oauth-provider-in-java –

Respuesta

14

Vas a querer familiarizarte con InputStreams y OutputStreams en Android, si has hecho esto en java normal antes, entonces es esencialmente lo mismo. Debe abrir una conexión con la propiedad de solicitud como "OBTENER", luego escribe sus parámetros en el flujo de salida y lee la respuesta a través de un flujo de entrada. Esto se puede ver en mi código de abajo:

 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()); 
    } 
+0

O puede usar este https://github.com/danielgindi/java-httprequest :-) –

+0

Simplemente no olvide muchas importaciones y dos declaraciones de variables. –

Cuestiones relacionadas