发布到服务器后获取响应 [英] Get Response after Posting to server

查看:121
本文介绍了发布到服务器后获取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要上传数据到服务器,我用下面的方法,成功地发送到服务器,在这里,我需要得到一个数字,它是注册ID。我有codeD,在服务器的PHP。我试图与邮差工作正常,但在这里我没有变。所以,请让我知道我有什么关系?

 静态字符串结果= NULL;
    私有静态无效后(字符串终点,地图<字符串,字符串> PARAMS)
            抛出IOException
        URL网址;
        尝试{
            URL =新的URL(端点);
        }赶上(MalformedURLException的E){
            抛出新抛出:IllegalArgumentException(无效URL:+终点);
        }
        StringBuilder的健美=新的StringBuilder();
        迭代器<钥匙进入LT;字符串,字符串>> 。迭代= params.entrySet()迭代();
        //构造使用的参数POST正文
        而(iterator.hasNext()){
            进入<字符串,字符串>参数= iterator.next();
            bodyBuilder.append(param.getKey())。追加('=')
                    .append(param.getValue());
            如果(iterator.hasNext()){
                bodyBuilder.append('和;');
            }
        }
        串体= bodyBuilder.toString();
        Log.v(TWA报告客户端,发帖+体+'给+网址);
        字节[]字节= body.getBytes();
        HttpURLConnection的康恩= NULL;
        尝试{
            康恩=(HttpURLConnection类)url.openConnection();
            conn.setDoOutput(真);
            conn.setUseCaches(假);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod(POST);
            conn.setRequestProperty(内容类型,
            应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8);
            //张贴请求
            OutputStream的OUT = conn.getOutputStream();
            out.write(字节);
            out.close();            //处理响应
            INT状态= conn.getResponse code();
            如果(状态!= 200){
              抛出新IOException异常(+状态后,错误code失败);
            }
        } {最后
            如果(参数conn!= NULL){
                conn.disconnect();
            }
        }      }


解决方案

试试这个片段,我会努力给你,

  URL网址;
URL =新的URL(http://www.url.com/app.php);
URLConnection的连接;
连接= url.openConnection();
HttpURLConnection的httppost =(HttpURLConnection类)连接;
httppost.setDoInput(真);
httppost.setDoOutput(真);
httppost.setRequestMethod(POST);
httppost.setRequestProperty(用户代理,瓦特兰海克-版本-t1.914);
httppost.setRequestProperty(Accept_Language,EN-US);
httppost.setRequestProperty(内容类型,
        应用程序/ x-WWW的形式urlen codeD);
DataOutputStream类DOS =新的DataOutputStream类(httppost.getOutputStream());
dos.write(二); //字节[] B后运行数据字符串回复;
InputStream的时间= httppost.getInputStream();
StringBuffer的SB =新的StringBuffer();
尝试{
    INT CHR;
    而((CHR = in.read())!= - 1){
        sb.append((char)的CHR);
    }
    回复= sb.toString();
} {最后
    附寄();
}

To post data to the server, I use the following method, successfully posts to the server, here I need to get one number that is registration id. I have coded that in the server php. I tried with Postman works fine, but here I'm not getting. So please let me know what I have to do?

static String result=null;    
    private static void post(String endpoint, Map<String, String> params)
            throws IOException {
        URL url;
        try {
            url = new URL(endpoint);
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("invalid url: " + endpoint);
        }
        StringBuilder bodyBuilder = new StringBuilder();
        Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
        // constructs the POST body using the parameters
        while (iterator.hasNext()) {
            Entry<String, String> param = iterator.next();
            bodyBuilder.append(param.getKey()).append('=')
                    .append(param.getValue());
            if (iterator.hasNext()) {
                bodyBuilder.append('&');
            }
        }
        String body = bodyBuilder.toString();
        Log.v("TWA CLIENT REPORT", "Posting '" + body + "' to " + url);
        byte[] bytes = body.getBytes();
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded;charset=UTF-8");
            // post the request
            OutputStream out = conn.getOutputStream();
            out.write(bytes);
            out.close();       

            // handle the response
            int status = conn.getResponseCode();
            if (status != 200) {
              throw new IOException("Post failed with error code " + status);
            }
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

      } 

解决方案

try this snippets, i will work to you,

URL url;
url = new URL("http://www.url.com/app.php");
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httppost = (HttpURLConnection) connection;
httppost.setDoInput(true);
httppost.setDoOutput(true);
httppost.setRequestMethod("POST");
httppost.setRequestProperty("User-Agent", "Tranz-Version-t1.914");
httppost.setRequestProperty("Accept_Language", "en-US");
httppost.setRequestProperty("Content-Type",
        "application/x-www-form-urlencoded");
DataOutputStream dos = new DataOutputStream(httppost.getOutputStream());
dos.write(b); // bytes[] b of post data

String reply;
InputStream in = httppost.getInputStream();
StringBuffer sb = new StringBuffer();
try {
    int chr;
    while ((chr = in.read()) != -1) {
        sb.append((char) chr);
    }
    reply = sb.toString();
} finally {
    in.close();
}

这篇关于发布到服务器后获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆