2011-07-21 18 views
6

Wysłałem metody w Objective-C wysyłania HTTP POST i w ciele mogę umieścić napis:HTTP POST z ciałem

NSString *requestBody = [NSString stringWithFormat:@"mystring"]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]]; 

teraz w Android chcę zrobić to samo i szukam sposób na ustawienie treści postu http.

Odpowiedz

4

Można użyć tego fragmentu -

HttpURLConnection urlConn; 
URL mUrl = new URL(url); 
urlConn = (HttpURLConnection) mUrl.openConnection(); 
... 
//query is your body 
urlConn.addRequestProperty("Content-Type", "application/" + "POST"); 
if (query != null) { 
urlConn.setRequestProperty("Content-Length", Integer.toString(query.length())); 
urlConn.getOutputStream().write(query.getBytes("UTF8")); 
} 
+0

i jak połączyć połączenie urlconnection z postem http? – MTA

+0

Dodałem więcej kodu powyżej, aby pomóc – Suchi

+6

Myślę, że trzeba dodać: 'urlConn.setDoOutput (true);' –

8

Możesz użyć HttpClient i HttpPost do zbudowania i wysłania żądania.

HttpClient client= new DefaultHttpClient(); 
HttpPost request = new HttpPost("www.example.com"); 

List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
pairs.add(new BasicNameValuePair("paramName", "paramValue")); 

request.setEntity(new UrlEncodedFormEntity(pairs)); 
HttpResponse resp = client.execute(request); 
+0

Klient.setEntity powinien być request.setEntity – Gorky

6

Można spróbować coś takiego za pomocą HttpClient i HttpPost:

List<NameValuePair> params = new ArrayList<NameValuePair>(); 
params.add(new BasicNameValuePair("mystring", "value_of_my_string")); 
// etc... 

// Post data to the server 
HttpPost httppost = new HttpPost("http://..."); 
httppost.setEntity(new UrlEncodedFormEntity(params)); 

HttpClient httpclient = new DefaultHttpClient(); 
HttpResponse httpResponse = httpclient.execute(httppost); 
0
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

następnie dodać elementy dla każdej pary

nameValuePairs.add(new BasicNameValuePair("yourReqVar", Value); 
nameValuePairs.add(.....); 

Następnie użyj HttpPost:

HttpPost httppost = new HttpPost(URL); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

i użyć HttpClient i Response aby uzyskać odpowiedź od serwera

1

Można użyć HttpClient i HttpPost wysłać ciąg json jako ciała:

public void post(String completeUrl, String body) { 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(completeUrl); 
    httpPost.setHeader("Content-type", "application/json"); 
    try { 
     StringEntity stringEntity = new StringEntity(body); 
     httpPost.getRequestLine(); 
     httpPost.setEntity(stringEntity); 

     httpClient.execute(httpPost); 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
} 

Json body body:

{ 
    "param1": "value 1", 
    "param2": 123, 
    "testStudentArray": [ 
    { 
     "name": "Test Name 1", 
     "gpa": 3.5 
    }, 
    { 
     "name": "Test Name 2", 
     "gpa": 3.8 
    } 
    ] 
} 
+0

dzięki @Guillaume do edycji formatu json – NNguyen