0

私はアンドロイドが初めてです。get、post、deleteの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

Here i post some code to post value to the server..

       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

アンドロイドでは、開発者が HttpUrlConnection を使用することをお勧めします...

そして、上記のjsonを文字列として必要とします。

ステップ 1) 新しい URL オブジェクトを作成します。

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

ステップ 2) HttpURLConnection オブジェクトを作成します。

HttpUrlConnection connection = url.openConnection();

ステップ 3) request プロパティを 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 に答える
0

上記のサンプルデータを使用したリクエスト構造は JSONObject であると想定しています。

ここで、JSONObject クラスを使用して JSONObject を作成するだけです。ここで、Web API とその Android への統合に関する私のプレゼンテーションの 1 つを見ることができます。

例:

    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 に答える