如何使用HttpUrl连接在android API 23中使用post发送名称值对对象或内容值对象? [英] How to send a name value pair Object or content values object using post in android api 23 using HttpUrl connection?

查看:81
本文介绍了如何使用HttpUrl连接在android API 23中使用post发送名称值对对象或内容值对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//这是我尝试过的. //我不知道如何附加inputurl和Content值,并使用HttpURLConnection将其发送到服务器.

//Here is what i have tried. // I don't know how to append inputurl and Content values and send this to server using HttpURLConnection.

公共JSONObject getJsonFromUrl(字符串inputurl,ContentValues值){

public JSONObject getJsonFromUrl(String inputurl, ContentValues values) {

    // Making http Request

    try {
        URL url=new URL(inputurl);

        HttpURLConnection connection=(HttpURLConnection)url.openConnection();
        connection.setReadTimeout(10000 /* milliseconds */);
        connection.setConnectTimeout(15000 /* milliseconds */);
        connection.setRequestMethod("POST");

        connection.setDoInput(true);

        connection.setDoOutput(true);

///请在此处插入代码以在URL后面附加值. connection.connect();

/// please insert the code here for appending values with url. connection.connect();

        //
        inputStream=connection.getInputStream();

    }
    catch (java.net.MalformedURLException e1){
        e1.printStackTrace();
    }
    catch (UnsupportedEncodingException e2) {
        e2.printStackTrace();
    }


    try {
        BufferedReader reader=new BufferedReader(new    InputStreamReader(inputStream,"iso-8859-1"),8);
        reader.read(getPost)
        StringBuilder builder=new StringBuilder();
        String line=null;

        while ((line=reader.readLine() !=null){
            builder.append(line + "n");
        }

        inputStream.close();
        jsonString=inputStream.toString();
        Log.e("JSON", jsonString);
    }
    catch (Exception e){
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try to parse the string into json object
    try {
        jsonObject=new JSONObject(jsonString);

    }
    catch (JSONException e){
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jsonObject;

}

}

推荐答案

如果您想通过HttpUrlConnection实例将JSON对象发布到url,则如下所示.

if you want to post a JSON object to an url though HttpUrlConnection instance you can write is like this.

    DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
    wr.writeBytes(jsonObject.toString());
    wr.flush();
    wr.close();
Where,

jsonObject is the json object which you would be posting to the URL 

整个代码可能看起来像这样

the entire code may look like this

URL url = new URL("this would be your url where you want to POST");

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
// when you are posting do make sure you assign appropriate header
// In this case POST.

httpURLConnection.setRequestMethod("POST");
httpURLConnection.connect();

// like this you can create your JOSN object which you want to send
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("email", "dddd@gmail.com");
jsonObject.addProperty("password", "password");

// And this is how you will write to the URL
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(jsonObject.toString());
wr.flush();
wr.close();

Log.d("TAG", "" + IOUtils.toString(httpURLConnection.getInputStream()));

这篇关于如何使用HttpUrl连接在android API 23中使用post发送名称值对对象或内容值对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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