How to send a name value pair Object or content va

2019-03-06 17:04发布

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

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);

/// 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;

}

}

1条回答
家丑人穷心不美
2楼-- · 2019-03-06 17:27

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", "ffffdd@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()));
查看更多
登录 后发表回答