如何处理服务器上的HttpURLConnection回调/响应

时间:2018-08-14 10:38:07

标签: java android

下面的代码工作得非常好,只是发现我很难处理回调响应并将其返回给用户

URL url = new URL(HttpUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");           
conn.setDoOutput(true);
conn.setDoInput(true);               
JSONObject jsonParam = new JSONObject();
jsonParam.put("phone", "254" + phone);
jsonParam.put("loanno", loanno);
jsonParam.put("amount", amount);
jsonParam.put("memberno", sharedPreferences.getString(Constants.UNIQUE_ID, ""));
Log.i("JSON", jsonParam.toString());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.writeBytes(jsonParam.toString());
os.flush();
os.close();
Log.i("STATUS", String.valueOf(conn.getResponseCode()));
Log.i("MSG", conn.getResponseMessage());
conn.disconnect();
progressDialog.dismiss();
finish();

0 个答案:

没有答案