2010-12-28 25 views
40

मुझे उपयोगकर्ता नाम और पासवर्ड के साथ उपयोगकर्ता को प्रमाणीकृत करने के लिए वेब-सेवा के लिए http पोस्ट अनुरोध करना है। वेब सेवा लड़के ने मुझे HTTP पोस्ट अनुरोध बनाने के लिए निम्नलिखित जानकारी दी।एंड्रॉइड, जावा: HTTP पोस्ट अनुरोध

POST /login/dologin HTTP/1.1 
Host: webservice.companyname.com 
Content-Type: application/x-www-form-urlencoded 
Content-Length: 48 

id=username&num=password&remember=on&output=xml 

एक्सएमएल प्रतिक्रिया है कि मैं हो रही होगी

<?xml version="1.0" encoding="ISO-8859-1"?> 
<login> 
<message><![CDATA[]]></message> 
<status><![CDATA[true]]></status> 
<Rlo><![CDATA[Username]]></Rlo> 
<Rsc><![CDATA[9L99PK1KGKSkfMbcsxvkF0S0UoldJ0SU]]></Rsc> 
<Rm><![CDATA[b59031b85bb127661105765722cd3531==AO1YjN5QDM5ITM]]></Rm> 
<Rl><![CDATA[[email protected]]]></Rl> 
<uid><![CDATA[3539145]]></uid> 
<Rmu><![CDATA[f8e8917f7964d4cc7c4c4226f060e3ea]]></Rmu> 
</login> 

यह मैं क्या कर रहा हूँ है HttpPost postRequest = नए HttpPost (urlString); मैं बाकी पैरामीटर कैसे बना सकता हूं?

उत्तर

81

यहां एक उदाहरण है जो पहले androidsnippets.com पर पाया गया था (साइट वर्तमान में अब और नहीं रखी गई है)।

// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); 

try { 
    // Add your data 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    // Execute HTTP Post Request 
    HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
} 

तो, आप अपने पैरामीटर BasicNameValuePair के रूप में जोड़ सकते हैं।

एक विकल्प (Http)URLConnection का उपयोग करना है। Using java.net.URLConnection to fire and handle HTTP requests भी देखें। यह वास्तव में नए एंड्रॉइड संस्करणों (जिंजरब्रेड +) में पसंदीदा तरीका है। this blog, this developer doc और एंड्रॉइड के HttpURLConnection javadoc देखें।

+1

क्या सरणी जोड़ने का कोई आसान तरीका है? क्या आपको उनके माध्यम से लूप करना होगा और जोड़ी BasicNameValuePair ("array []", array [i]) जोड़ना होगा? – gsingh2011

+1

क्या यह एक्सएमएल में जेएसओएन फाइलों पर भी प्रभावी है? –

+2

एंड्रॉइड 2.3 और उच्चतर के लिए Google HttpURLConnection का उपयोग करने की अनुशंसा करता है। http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html –

0

जावा के लिए HttpClient का प्रयास करें:

http://hc.apache.org/httpclient-3.x/

+1

क्या आपको 'एंड्रॉइड' टैग याद आया? यह पहले से ही कवर के तहत HttpClient (एक लाइट संस्करण) का उपयोग कर रहा है! [एचटीपीपोस्ट जावाडोक] (http://developer.android.com/reference/org/apache/http/client/methods/HttpPost.html) भी देखें। – BalusC

2

HttpPost उपयोग करने पर विचार करें। इस से अपनाएं: http://www.javaworld.com/javatips/jw-javatip34.html

URLConnection connection = new URL("http://webservice.companyname.com/login/dologin").openConnection(); 
// Http Method becomes POST 
connection.setDoOutput(true); 

// Encode according to application/x-www-form-urlencoded specification 
String content = 
    "id=" + URLEncoder.encode ("username") + 
    "&num=" + URLEncoder.encode ("password") + 
    "&remember=" + URLEncoder.encode ("on") + 
    "&output=" + URLEncoder.encode ("xml"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

// Try this should be the length of you content. 
// it is not neccessary equal to 48. 
// content.getBytes().length is not neccessarily equal to content.length() if the String contains non ASCII characters. 
connection.setRequestProperty("Content-Length", content.getBytes().length); 

// Write body 
OutputStream output = connection.getOutputStream(); 
output.write(content.getBytes()); 
output.close(); 

आप अपवाद अपने आप को पकड़ने के लिए की आवश्यकता होगी।

+0

मैं प्रतिक्रिया कैसे मुद्रित करूं? – Tamil

0

आप कार्यान्वयन मैं ACRA को जोड़ा गया पुन: उपयोग कर सकते हैं: http://code.google.com/p/acra/source/browse/tags/REL-3_1_0/CrashReport/src/org/acra/HttpUtils.java?r=236

(, doPost (मानचित्र, यूआरएल) विधि देखें से अधिक काम कर रहे http और यहां तक ​​कि स्वयं पर हस्ताक्षर किए प्रमाणपत्र के साथ https)

5

@BalusC जवाब देने के लिए मैं एक स्ट्रिंग में प्रतिक्रिया को परिवर्तित करने के तरीके को जोड़ूंगा:

HttpResponse response = client.execute(request); 
HttpEntity entity = response.getEntity(); 
if (entity != null) { 
    InputStream instream = entity.getContent(); 

    String result = RestClient.convertStreamToString(instream); 
    Log.i("Read from server", result); 
} 

Here is an example of convertStramToString

0

मैं निम्नलिखित कोड का इस्तेमाल किया मेरे सर्वर पर सी # डेस्कटॉप ऐप्लिकेशन के लिए अपने एंड्रॉयड ग्राहक app से HTTP POST भेजने के लिए:

// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); 

try { 
    // Add your data 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    // Execute HTTP Post Request 
    HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
} 

मैं (एक तरह कुछ मेरे सर्वर पर एक सी # app से अनुरोध पढ़ने पर काम किया वेब सर्वर थोड़ा आवेदन)। मैं अनुरोध को पढ़ने के लिए प्रबंधित निम्नलिखित कोड का उपयोग कर तैनात डेटा:

server = new HttpListener(); 
server.Prefixes.Add("http://*:50000/"); 
server.Start(); 

HttpListenerContext context = server.GetContext(); 
HttpListenerContext context = obj as HttpListenerContext; 
HttpListenerRequest request = context.Request; 

StreamReader sr = new StreamReader(request.InputStream); 
string str = sr.ReadToEnd(); 
1

मैं नहीं बल्कि, आप वॉली उपयोग करने के लिए प्राप्त करने के लिए, डाल सलाह देते हैं पोस्ट ... अनुरोध।

सबसे पहले, अपनी gradle फ़ाइल में निर्भरता जोड़ें।

compile 'com.he5ed.lib:volley:android-cts-5.1_r4'

अब, अनुरोध करने के लिए इस कोड स्निपेट का उपयोग करें।

RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); 

     StringRequest postRequest = new StringRequest(com.android.volley.Request.Method.POST, mURL, 
       new Response.Listener<String>() 
       { 
        @Override 
        public void onResponse(String response) { 
         // response 
         Log.d("Response", response); 
        } 
       }, 
       new Response.ErrorListener() 
       { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         // error 
         Log.d("Error.Response", error.toString()); 
        } 
       } 
     ) { 
      @Override 
      protected Map<String, String> getParams() 
      { 
       Map<String, String> params = new HashMap<String, String>(); 
       //add your parameters here as key-value pairs 
       params.put("username", username); 
       params.put("password", password); 

       return params; 
      } 
     }; 
     queue.add(postRequest); 
संबंधित मुद्दे