2012-06-26 18 views
7

में अनुरोध मैं android.So करने के लिए नए मैं मुझे थानेदार किसी भी एक कैसे एक http एंड्रॉयड (जावा) मेंकैसे HTTP GET एंड्रॉयड

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" 

के रूप में अनुरोध ऐसे मिल बनाने के लिए कर सकते हैं कर रहा हूँ बनाने के लिए?

+4

मुझे विश्वास है कि यह आप का जवाब दे देंगे: http://stackoverflow.com/questions/3505930/make-in-http-request-with-android – DRAX

+0

लेकिन जहां क्या मैं oauth_consumer_key, आदि जैसे सभी पैरामीटर डालूंगा ... –

+0

यह आपको शपथ के साथ शुरू करना चाहिए -> http://stackoverflow.com/questions/2150801/implementing-oauth-provider-in-java –

उत्तर

14

आप एंड्रॉइड में इनपुटस्ट्रीम और आउटपुटस्ट्रीम से परिचित होना चाहते हैं, अगर आपने नियमित रूप से जावा में ऐसा किया है तो यह अनिवार्य रूप से वही बात है। आपको अनुरोध संपत्ति के साथ "GET" के रूप में कनेक्शन खोलने की आवश्यकता है, फिर आप आउटपुट स्ट्रीम में अपने पैरामीटर लिखें और इनपुट स्ट्रीम के माध्यम से प्रतिक्रिया पढ़ें। आप नीचे दिए गए मेरे कोड में देख सकते हैं:

 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

या आप इस का उपयोग कर सकते हैं https://github.com/danielgindi/java-httprequest :-) –

+0

बस बहुत सारे आयात और दो परिवर्तनीय घोषणाओं को न भूलें। –

संबंधित मुद्दे