http链接
工作流程
一次HTTP操作称为一个事物,某工作过程可分为四步:
1.首先客户机与服务器需要建立连接,只要单击某个超级链接,HTTP的工作开始
2.建立连接后,客户机发送一个请求给服务器,请求方式的格式为:统一资源标识符(URL)、协议版本号,后边是MIME信息包括请求修饰符、客户机信息和可能的内容,
3.服务器接到请求后,给与响应信息,其格式为一个状态行,包括信息的协议版本号、一个成功或错误的代码,后边是MIME信息包括服务器信息、实体信息和可能的内容。
4.客户端接受服务器所返回的信息通过浏览器显示的显示屏上,然后客户机与服务器断开连接
更多网络编程和协议详细看https://blog.csdn.net/xiaoxin0630/article/details/88638624
使用HttpURLConection 进行连接
//获取一个实体的url
URL url = new URL("http://192.168.xxx.xxx:8080/transportservice/type/jason/action/SetParkRate.do?'Money':"+edt_fare.getText().toString());
//从URL中实例化一个HttpURLConnection对象
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
Toast.makeText(getApplicationContext(),"修改完成",Toast.LENGTH_LONG).show();
JSON
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("name","http在安卓中的应用");
jsonObject.put("author","Nick");
JSONArray jsonArray=new JSONArray();
jsonArray.put("http基础");
jsonArray.put("json数据解析");
jsonObject.put("content",jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
mTextView.setText(jsonObject.toString());
@Override
public void onResponse(JSONObject response) {
try {
//tv.setText(response.getString("name"));
JSONArray jsonArray = response.optJSONArray("data");
arrayList = new ArrayList();
if(jsonArray != null && jsonArray.length() > 0){
//±éÀúJSONÊý×éµÃµœÆäÖеÄcityÊýŸÝ
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
News news = new News();
news.setId(jsonObject.getString("id"));
news.setTime(jsonObject.getString("time"));
news.setTitle(jsonObject.getString("title"));
news.setSketch(jsonObject.getString("sketch"));
news.setDetailed(jsonObject.getString("detailed"));
arrayList.add(news);
Log.d("Test", jsonObject.getString("id"));
Log.d("Test", jsonObject.getString("time"));
Log.d("Test", jsonObject.getString("title"));
Log.d("Test", jsonObject.getString("sketch"));
Log.d("Test", jsonObject.getString("detailed"));
}
}
}catch (JSONException e){
e.printStackTrace();
}
}