如何使httppost请求

问题描述:

我是新来的android。我正在学习http请求获取,发布,删除。从这我学到了获取和删除,并发布请求。 但是在发送请求中发送数组时发生问题。如何使httppost请求

这是我的职位数据结构..

{ 
"customerId": "CUST01", 
"orderId": "101010", 
"orderTotal": 99.99, 
"orderDetailList": [ 
    { 
    "lineId": "1", 
    "itemNumber": "ABC", 
    "quantity": 9, 
    "price": 10.0 
    }, 
    { 
    "lineId": "2", 
    "itemNumber": "XYZ", 
    "quantity": 1, 
    "price": 9.99 
    } 
] 
} 

如何在邮寄给数组?

我假设你的请求结构是JSONObject与上面给出的示例数据。

现在,只需创建一个JSONObject用的JSONObject类的帮助下,在这里你可以通过我的演讲之一Web API & Its Integration in Android.

例子:

JSONObject myJSONRequest = new JSONObject(); 
    myJSONRequest.put("customerId", "CUST01"); 
    myJSONRequest.put("orderId","101010"); 
    ......... 
    ......... 
    JSONArray arrayOrder = new JSONArray(); 
    for(int i=cntLine; i<n; i++) 
    { 
     JSONObject objSub = new JSONObject(); 
     objSub .put("lineId", String.valueOf(i)); 
     objSub .put("itemNumber", String.valueOf(i)); 
     ............. 
     ............. 

     arrayOrder.put(objSub); 
    } 
    myJSONRequest.put("orderDetailList", arrayOrder.toString()); 



    // create complete request object by placing all the values inside it. 


    // Below code is for posting request data to web api 

    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(url); 
    post.setEntity(new StringEntity(myJSONRequest.toString(), "utf-8")); 
    HttpResponse response = client.execute(post); 
+0

我棒使用jsonobject.put或jsonarray.put – user2071490 2013-03-20 07:17:59

+0

和阵列 “orderDetailList”:[{ “lineId”: “1”, “itemNumber”: “ABC”, “量”:9, “价格“:10。0 }, { “lineId”: “2”, “itemNumber”: “XYZ”, “量”:1, “价格”:9.99 } ] – user2071490 2013-03-20 07:25:00

+0

可以请您给我解释一下我怎么能以这种格式构建一个数组? 例如, “orderDetailList”:[ { “lineId”: “1”, “itemNumber”: “ABC”, “量”:9, “价格”:10.0 }, { “lineId”:“ 2" , “itemNumber”: “XYZ”, “量”:1, “价格”:9.99 } ] 在这种情况下我怎样才能建立一个数组? – user2071490 2013-03-20 07:36:09

这里我发布了一些如何从url获取内容的代码。

现在,您必须在Array中传递String。

try{ 


      HttpClient httpclient = getNewHttpClient(); 

      HttpGet httpget = new HttpGet("url"); 


      HttpResponse response = httpclient.execute(httpget); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

     }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 

     //convert response to string 
     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result=sb.toString(); 
     }catch(Exception e){ 
      Log.e("log_tag", "Error converting result "+e.toString()); 
     } 
+1

'HTTPGET httppost'?错误的命名约定,加上OP希望“发布”一些数据,而不是“GET”。 – SudoRahul 2013-03-20 07:08:55

+0

OP想要发布数据,并在这里发布了获取数据的答案。 – 2013-03-20 07:11:54

+0

这是我用过的get方法。 – user2071490 2013-03-20 07:14:39

下面我发布一些代码张贴价值服务器..

 public void postData() { 
     // 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 
//you can add all the parameters your php needs in the BasicNameValuePair. 
//The first parameter refers to the name in the php field for example 
// $id=$_POST['customerId']; the second parameter is the value. 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair("customerId", "CUST01")); 
nameValuePairs.add(new BasicNameValuePair("orderId", "101010")); 
    nameValuePairs.add(new BasicNameValuePair("orderTotal", "99.99")); 
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 
    }} 
+0

NameValuePair ?? – 2013-03-20 07:16:39

+0

jsonobject.put不是NameValuePair,我知道 – user2071490 2013-03-20 07:21:44

+0

检查此链接也http://www.vogella.com/articles/ApacheHttpClient/article.html – user1835052 2013-03-20 07:23:00

在安卓其建议开发者使用HttpURLConnection的...

而你需要上面的json作为字符串。

步骤1)创建新的URL对象。

URL url = new URL("www.url.com/demoservice"); 

步骤2)创建HttpURLConnection对象。

HttpUrlConnection connection = url.openConnection(); 

步骤3)设置请求属性HttpPost ..

connection.setRequestProperty("request-Method","POST"); 
connection.setRequestProperty("content-type","application/json"); 

步骤4)获取输出流参考。

OutputStream stream = connection.getOutputStream(); 

步骤5)将json字符串写入输出流。

stream.write(jsonString.toBytes()); 

步骤6)关闭流。

stream.close(); 

希望这将是有益的..