我是android新手。我正在学习获取,发布,删除的http请求。从此我学会了获取和删除,并发布请求。 但是在post请求中发送数组时出现了问题。
这是我的帖子数据结构..
{
"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
}
]
}
如何在帖子中发送数组?
答案 0 :(得分: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
}}
答案 1 :(得分:0)
在这里,我发布了一些如何从网址获取内容的代码。
现在你必须在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());
}
答案 2 :(得分:0)
我假设您的请求结构是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);
答案 3 :(得分:0)
在android中它建议开发人员使用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();
希望这会有所帮助..