Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中通过POST方法在HttpURLConnection中传递JsonObject?_Android_Androidhttpclient - Fatal编程技术网

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

如何在Android中通过POST方法在HttpURLConnection中传递JsonObject?,android,androidhttpclient,Android,Androidhttpclient,当我使用GET方法时,就可以成功运行了。我的代码如下: HttpURLConnection http = null; URL url; try { url = new URL(URLPREFIX + "login?user=" + "username" + "&pwd=" + "password"); Log.e(TAG, "Login url :" + url.toS

当我使用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方法时如何操作。任何人都请帮助我。我花了很多时间在这段代码后面。但我尝试的结果是零。请帮帮我

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

你说的操纵是什么意思?要发送或获取json对象吗?将json对象发送到服务器。