简体   繁体   English

ANDROID-如何使用httpurlconnection从android中的Web服务获得成功返回?

[英]ANDROID - How to get return success from web service in android using httpurlconnection?

I've a return 0 from web services using postman if the data send successfully. 如果数据发送成功,我将使用邮递员从Web服务返回0。

but I'm quite confused how to detect 0 message in android using HttpURLConnection 但是我很困惑如何使用HttpURLConnection在android中检测0消息

in HttpClient I'm using String response = httpclient.execute(httppost, responseHandler); HttpClient我正在使用String response = httpclient.execute(httppost, responseHandler);

String response = httpclient.execute(httppost, responseHandler);
Log.d("MainActivity", "INSERT:" + response);

but refer to the docs 但请参考文档

there's some code like getResponseCode() getResponseMessage() but the output is 200 for getResponseCode() and OK for getResponseMessage() 有没有像一些代码getResponseCode() getResponseMessage()但输出是200getResponseCode()OKgetResponseMessage()

so how to get output of 0 in HttpURLConnection ? 那么如何在HttpURLConnection获得0输出呢?

EDIT urlconnection code: 编辑 urlconnection代码:

try {
    JSONObject job = new JSONObject(log);
    String param1 = job.getString("AuditScheduleDetailID");
    String param2 = job.getString("AuditAnswerId");
    String param3 = job.getString("LocalFindingID");
    String param4 = job.getString("LocalMediaID");
    String param5 = job.getString("Files");
    String param6 = job.getString("ExtFiles");
    Log.d("hasil json", param1 + param2 + param3 + param4 + param5 + param6 + " Kelar id " +
            "pertama");

    URL url = new URL("myurl");
    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("AuditScheduleDetailID", param1);
    jsonParam.put("AuditAnswerId", param2);
    jsonParam.put("LocalFindingID", param3);
    jsonParam.put("LocalMediaID", param4);
    jsonParam.put("Files", param5);
    jsonParam.put("ExtFiles", param6);

    Log.i("JSON", jsonParam.toString());
    DataOutputStream os = new DataOutputStream(conn.getOutputStream());
    //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
    os.writeBytes(jsonParam.toString());

    os.flush();
    os.close();

    int respon = conn.getResponseCode();
    Log.i("STATUS", String.valueOf(conn.getResponseCode()));
    Log.i("Input", String.valueOf(conn.getInputStream()));
    Log.i("MSG", conn.getResponseMessage());

    conn.disconnect();
} catch (JSONException | IOException e) {
    e.printStackTrace();
}

This is how you need to read the data from the server using HttpUrlConnection: 这是您需要使用HttpUrlConnection从服务器读取数据的方式:

try {
    JSONObject job = new JSONObject(log);
    String param1 = job.getString("AuditScheduleDetailID");
    String param2 = job.getString("AuditAnswerId");
    String param3 = job.getString("LocalFindingID");
    String param4 = job.getString("LocalMediaID");
    String param5 = job.getString("Files");
    String param6 = job.getString("ExtFiles");
    Log.d("hasil json", param1 + param2 + param3 + param4 + param5 + param6 + " Kelar id " +
            "pertama");

    URL url = new URL("myurl");
    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("AuditScheduleDetailID", param1);
    jsonParam.put("AuditAnswerId", param2);
    jsonParam.put("LocalFindingID", param3);
    jsonParam.put("LocalMediaID", param4);
    jsonParam.put("Files", param5);
    jsonParam.put("ExtFiles", param6);

    Log.i("JSON", jsonParam.toString());
    DataOutputStream os = new DataOutputStream(conn.getOutputStream());
    //os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
    os.writeBytes(jsonParam.toString());

    os.flush();
    os.close();
    InputStream is = null;

    if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
            is = conn.getInputStream();// is is inputstream
        } else {
            is = conn.getErrorStream();
        }


        try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        String response = sb.toString();
        //HERE YOU HAVE THE VALUE FROM THE SERVER
        Log.d("Your Data", response);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }


    conn.disconnect();
} catch (JSONException | IOException e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中使用HttpURLConnection类将JSON对象发送到Web服务? - How to send a JSON Object to a web service using HttpURLConnection class in Android? android使用HttpURLConnection请求一个php web服务并提交php表单 - android request a php web service using HttpURLConnection and submit the php form 使用Android HttpURLConnection 获取Http - Http Get using Android HttpURLConnection 如何使用HttpURLConnection确定Web服务状态? - How to determine the web service status using HttpURLConnection? 如何使用httpUrlConnection获取url的查询字符串 - How to get a query string for a url using httpUrlConnection , android 如何在使用HttpURLConnection(android / java)发布之前获取VIEWSTATE? - How to get VIEWSTATE before posting using HttpURLConnection (android/java)? HttpUrlConnection-Android应用-带有Spring Web Service的oauth2授权 - HttpUrlConnection - android app - oauth2 authorization with spring web service Android Java:从HttpUrlConnection获取响应正文 - Android Java: Get Response Body from HttpUrlConnection 如何从phonegap插件获取android服务返回消息 - How to get an android service return message from a phonegap plugin 如何使用HttpUrlConnection在android中构建REST客户端 - how to build REST client in android using HttpUrlConnection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM