0
 HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
     con.setDoOutput(true); 
     con.setDoInput(true); 
     con.setRequestMethod("POST"); 
     con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
     con.setRequestProperty("Accept", "application/json"); 

     OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream()); 
     wr.write(data.toString()); 
     wr.flush(); 
     status = con.getResponseCode(); 

此代码是Android Studio中的AsyncTask类的一部分。当我使用PostMan执行Post请求时,返回的状态是200;但是,当我使用android模拟器发出请求时,状态将返回为0.我感到非常困惑,并且需要一些帮助。在AsyncTask中调用HttpUrlConnection.getResponseCode()时获取状态码0

回答

0

状态码0表示某种网络错误。 尝试检查:

  • Internet权限
  • 仿真器,连接
+0

我有上网权限和连接;当我执行POST请求时,数据被正确发送(它正确地发送到数据库)。但是,状态码返回为0。 –