HttpUrlConnection获取服务器数据,并返回数据
2017-04-14 14:15
459 查看
public class MainActivity extends AppCompatActivity { private Button button; public Handler handlerJson = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: break; case 1: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { httpPost(); } }); } /** * @function http请求 */ private void httpPost() { final StringBuffer responseResult = new StringBuffer(); new Thread() { @Override public void run() { super.run(); JSONObject ob = new JSONObject(); try { ob.put("nickName", "123456789"); ob.put("passWord", "123456789"); } catch (JSONException e) { e.printStackTrace(); } URL url = null; try { url = new URL("http://172.18.74.9:8088/hello"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-type", "application/json;charset=UTF-8"); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); /**发送POST请求必须设置如下两行*/ connection.setDoInput(true); connection.setDoOutput(true); //设置连接时间为10秒 connection.setConnectTimeout(10000); connection.connect(); /**获取URLConnection对象对应的输出流*/ PrintWriter mprintwriter = new PrintWriter(connection.getOutputStream()); mprintwriter.write(ob.toString()); mprintwriter.flush(); int responseCode = connection.getResponseCode(); if (responseCode != 200) { Log.e("tag", "连接错误:错误码为:" + responseCode); Message msg = Message.obtain(); msg.what = 0; msg.obj = responseCode; handlerJson.sendMessage(msg); } else { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { responseResult.append("\n").append(line); } Message msg = Message.obtain(); msg.what = 1; msg.obj = responseResult.toString(); Log.e("tag", "http返回数据为:" + responseResult.toString()); handlerJson.sendMessage(msg); Log.e("tag", "连接成功"); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } }
相关文章推荐
- HttpUrlConnection Post提交数据到服务器、并得到服务器返回的数据
- HttpUrlConnection Post提交数据到服务器、并得到服务器返回的数据
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- java httpURL连接远程服务器并返回数据(httpurlconnection)(转)
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- 通过HttpURLConnection连接服务器,发送报文,获取服务器报文返回
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- HttpClient和HttpURLConnection获取服务器返回的内容
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- HttpURLConnection---从服务器获取数据
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- 通过api使用HttpURLConnection 获取返回天气数据。
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- java httpURL连接远程服务器并返回数据(httpurlconnection)
- android将InputStream转为FileOutputStream保存HttpURLConnection获取到的数据
- Android使用HttpURLConnection获取数据
- 使用 HttpURLConnection 获取不到网络数据