无法使用httpurlconnection发布数据,在Android中找不到404个文件



我已经编写了一个代码来注册用户或发布一些数据,但是我无法发布数据,因为找不到404个文件。

Class A.java 
Map<String, Object> param = new LinkedHashMap<>();
param.put("UserName", user.getUser_name());
param.put("Password", user.getPassword());
param.put("EmailId", user.getEmailid());
param.put("MobileNo", "NA");
String str_url = 
"http://syncroft.in/SupervisorProductivity/RegisterUser"
AsyncTaskPost asyncTaskPost = new 
AsyncTaskPost(context,param,str_url,Register.this);
asyncTaskPost.execute();

AsyncTaskPost.java
@Override
protected String doInBackground(Integer... integers) {
    try {
        JSONObject jsonObject = CustomHttpClient.post(url, 
        localparams);
        jsonresult = jsonObject.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
CustomHttpClient.java
public static JSONObject post(String str_url, Map<String,Object> 
    params) throws Exception {
    StringBuilder postData = new StringBuilder();
    for (Map.Entry<String,Object> param : params.entrySet()) {
        if (postData.length() != 0) postData.append('&');
        postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
        postData.append('=');
        postData.append(URLEncoder.encode(String.valueOf
        (param.getValue()), 
        "UTF-8"));
    }
    byte[] postDataBytes = postData.toString().getBytes("UTF-8");
    URL url = new URL(str_url);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Length", 
    String.valueOf(postDataBytes.length));
    conn.setDoOutput(true);
    DataOutputStream dStream = new 
    DataOutputStream(conn.getOutputStream());
    dStream.writeBytes(postData.toString());
    dStream.flush();
    dStream.close();
    int responseCode = conn.getResponseCode();
    BufferedReader br = new BufferedReader(new 
    InputStreamReader(conn.getInputStream()));
    String line = "";
    StringBuilder returnvalue = new StringBuilder();
    while((line = br.readLine())!= null){
        returnvalue.append(line);
    }
    br.close();
    JSONObject responseObj = new JSONObject(returnvalue.toString());
    conn.disconnect();
    return responseObj;
}

任何人都可以告诉我,我在哪里出错,因为我将404作为响应代码,文件未找到例外。上述URL在Postman中工作,但在Java代码中不起作用。

也许您只是调用错误的API,或者这是服务器端必须处理此问题的API问题。

最新更新