0

我是安卓新手。我正在学习获取、发布、删除的 http 请求。从中我学会了获取和删除以及发布请求。但是在发布请求中发送数组时出现问题。

这是我的帖子数据结构..

{
 "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
   }
 ]
}   

如何在帖子中发送数组?

4

4 回答 4

1

在这里,我发布了一些代码以将值发布到服务器..

       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
  }}
于 2013-03-20T07:15:01.490 回答
0

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

现在你必须在数组中传递字符串。

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());
        }
于 2013-03-20T07:07:31.250 回答
0

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

现在,只需在 JSONObject 类的帮助下创建一个 JSONObject,在这里您可以浏览我的一个关于Web API 及其在 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);
于 2013-03-20T07:11:11.517 回答
0

在 android 中,它建议开发人员使用 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();

希望这会有所帮助..

于 2013-03-20T08:30:52.990 回答