Json格式的http請求
阿新 • • 發佈:2019-01-02
服務端php程式碼可以從這裡下載:https://github.com/lornajane/PHP-Web-Services
1.使用volley實現:
request要用JsonObjectRequest,這個request在url後面帶有一個JSONObject型別的引數
如果服務端有檢測http頭的請求資料型別和接受資料型別(比如有的服務端會根據http頭中的Accept欄位標示的型別,返回json,xml或其他資料型別,也會根據Content-type欄位的標示,按json或form型別解析請求引數),還需要在getHeaders回撥中設定頭部引數,如果服務端不檢測,可以不設定:
headers.put("Accept", "application/json");//表示接受json型別的響應 headers.put("Content-Type", "application/json; charset=UTF-8");//表示傳遞給伺服器的引數是json型別
String strUrl = "http://10.2.152.133/test/rest/rest.php/items"; try { JSONObject jsonBody = new JSONObject("{\"name\":\"Brett\",\"link\":\"haha\"}"); JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, strUrl, jsonBody, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d("qf", response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("qf", error.getMessage()); } } ) { @Override public Map<String, String> getHeaders() { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Accept", "application/json"); headers.put("Content-Type", "application/json; charset=UTF-8"); return headers; } }; MyApp.mRequestQueue.add(request); } catch (Exception e) { e.printStackTrace(); }
2.使用httputils實現:
content-type設定也要根據情況需要,同volley一樣
普通的表單請求是addQueryStringParameter(get)或addBodyParameter(post),json請求是setBodyEntity
protected void postJsonByXutils() { String strUrl = "http://10.2.152.133/test/rest/rest.php/items"; RequestParams requestParams = new RequestParams(); requestParams.addHeader("Content-Type", "applicasettion/json"); JSONObject json = new JSONObject(); try { json.put("name", "zy"); json.put("link", "zy2"); } catch (JSONException e) { e.printStackTrace(); } requestParams.setBodyEntity(new StringEntity(json.toString(), "utf-8")); MyApp.mHttpUtils.send(HttpRequest.HttpMethod.POST, strUrl,requestParams,new RequestCallBack<String>() { @Override public void onSuccess(ResponseInfo<String> responseInfo) { String strResult = responseInfo.result; Log.d("qf", strResult); } @Override public void onFailure(HttpException error, String msg) { Log.d("qf", msg); } }); }
3.使用httpurlconnection:
protected void postJsonByHttpURLConnection() { HttpURLConnection httpcon; String url = "http://10.2.152.133/test/rest/rest.php/items"; String data = "{\"name\":\"Brett2\",\"link\":\"haha2\"}"; String result = null; try { //Connect httpcon = (HttpURLConnection) ((new URL(url).openConnection())); httpcon.setDoOutput(true); httpcon.setRequestProperty("Content-Type", "application/json"); httpcon.setRequestProperty("Accept", "application/json"); httpcon.setRequestMethod("POST"); httpcon.connect(); //Write OutputStream os = httpcon.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(data); writer.close(); os.close(); //Read BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream(), "UTF-8")); String line = null; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); } br.close(); result = sb.toString(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }