如何在Android中通过POST方法在HttpURLConnection中传递JsonObject?

时间:2014-05-04 17:26:47

标签: android androidhttpclient

当我使用GET方法然后成功运行。我的代码如下:

HttpURLConnection http = null;
        URL url;
        try {
            url = new URL(URLPREFIX + "login?user=" + "username" + "&pwd="
                    + "password");
            Log.e(TAG, "Login url :" + url.toString());
            HttpsURLConnection https = (HttpsURLConnection) url
                    .openConnection();
            reader = new BufferedReader(new InputStreamReader(
                    http.getInputStream()));
            Log.e(TAG, "reader :" + reader.toString());
            StringBuilder sb = new StringBuilder();
            String line = null;
            // Read Server Response
            while ((line = reader.readLine()) != null) {
                // Append server response in string
                sb.append(line + "\n");
            }
            Log.e(TAG, "sb :" + sb.toString());
            JSONObject job = new JSONObject(sb.toString());
            Log.e(TAG, "job :" + job.toString());
            mSuccess = job.getInt("Status");
            if (mSuccess == 1) {

            } else if (mSuccess == 0) {

            } else if (mSuccess == 2) {

            } else {


            }

        } catch (MalformedURLException e) {
            Log.e(TAG, "MalformedURLException" + e.toString());
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "IOException" + e.toString());
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            Log.e(TAG, "JSONException" + e.toString());
            e.printStackTrace();
        } finally {
            try {

                reader.close();
            } catch (Exception ex) {
            }
        }

但我不知道如何操纵POST方法。假设

    url :https/example.com/folder/something
    parameters  : JsonObject. such as
    {
    "foldername" : "imageFolder",
    "jsonArray"  : ["abc","sdsf","sfsd"],
    "location"   : "Dhaka"
    }

使用post方法时如何操作。请帮助我任何人。我花了很多时间杀死这段代码。但我尝试的输出为零。请帮帮我。

1 个答案:

答案 0 :(得分:0)

private static HttpResponse sendRequest(String url, String stringParams, Header[] headers)

            if(stringParams!=null) {
        Log.d(TAG, "sending request with the given params:" + stringParams);
    }
    HttpResponse response = null;
    HttpRequestBase request = null;
    DefaultHttpClient client = new DefaultHttpClient();

    HttpEntity entity =getHttpEntity(stringParams);
        request = new HttpPost(url);
        if (entity != null) {
            ((HttpPost) request).setEntity(entity);
        }


    Log.d(TAG, "request to url: " + url);
    if (headers != null) {
        Log.d(TAG, "adding headers: " + headers);
        request.setHeaders(headers);
    } else {
        Log.d(TAG, "no headers to add");
    }
    try{
        response = client.execute(request);
    } catch (Exception e) {
        Log.w(TAG, "exception", e);
        throw e;
    } finally {
        client.shutdown();
    }
    return response;
}


private static HttpEntity getHttpEntity(String stringEntity) {
        HttpEntity entity = null;
        try {
            entity = new StringEntity(stringEntity, HTTP.UTF_8);
            ((StringEntity) entity).setContentType("application/json;charset=UTF-8");
            ((StringEntity) entity).setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));

        } catch (Exception e) {
            Log.d(TAG, "error creating entity: " + stringEntity);
        }
        return entity;
    }

你需要将你的jsonObject转换为字符串,使用这个方法,只需调用jsonObject.toString();