【问题标题】:Json not working with HttpPost probably around setEntityJson 不使用 HttpPost 可能围绕 setEntity
【发布时间】:2013-01-02 09:15:41
【问题描述】:

我正在使用此代码将其发送到我的 php 文件。 该文件看起来像这样。

file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true));

我正在发送这样的 json:

public void postData(String url,JSONObject json) {
    HttpClient httpclient = new DefaultHttpClient();
    String object = "?data=" +json.toString();
    System.out.println("object:" + object);
    try {
        HttpPost httppost = new HttpPost(url.toString());
        httppost.setHeader("Content-type", "application/json");
        System.out.println("url:" + url.toString());
        StringEntity se = new StringEntity(object); 
        System.out.println("json:" + json.toString());

        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se); 

        HttpResponse response = httpclient.execute(httppost);
        String temp = EntityUtils.toString(response.getEntity());
        System.out.println("response:" + response);

        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println("Statuscode " + statusCode);

        if(statusCode==200) {
            Toast.makeText(LbsGeocodingActivity.this, "Verstuurd",  Toast.LENGTH_LONG).show();
        }
        else if(statusCode!=200) {
            Toast.makeText(this, statusCode, Toast.LENGTH_LONG).show();
        }
        /* try {
                String responseBody = EntityUtils.toString(response.getEntity());
        Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();
        } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }*/
    } 
    catch (ClientProtocolException e) {

    } 
    catch (IOException e) {
    }
}

日志文件是这样的:

01-08 09:36:00.278: I/System.out(1124): object:?data={"id":"69403","timestamp":"08-01-2013 09:36:00","longitude":"-122.084095","latitude":"37.422005"}
01-08 09:36:00.298: I/System.out(1124): json:{"id":"69403","timestamp":"08-01-2013 09:36:00","longitude":"-122.084095","latitude":"37.422005"}
01-08 09:36:01.038: I/System.out(1124): response:org.apache.http.message.BasicHttpResponse@412d1b38
01-08 09:36:01.038: I/System.out(1124): Statuscode 200

出于安全原因,我省略了网址..

但我在 dump.txt 中得到一个空文件

POST: 
Array
(
)



 GET: 
Array
(
)

编辑

setEntity(或附近某处)出了点问题,因为当我已经将 ?data= 粘贴到 url 中时,我从 PHP 文件中得到了这个:

POST: 
Array
(
)



 GET: 
Array
(
    [data] => 
)

【问题讨论】:

    标签: android json entity http-post


    【解决方案1】:

    我在我的应用程序中使用DefaultHttpClient 发送 JSON 的方式非常有效:

    public static HttpUriRequest createPostForJSONObject(
            JSONObject params, String url) {
        HttpPost post = new HttpPost(url);
        post.setEntity(createStringEntity(params));
        return post;
    }
    
    private static HttpEntity createStringEntity(JSONObject params) {
        StringEntity se = null;
        try {
            se = new StringEntity(params.toString(), "UTF-8");
            se.setContentType("application/json; charset=UTF-8");
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "Failed to create StringEntity", e);
            exception = e;
        }
        return se;
    }
    

    为了在您的代码中运行此请求,您需要做的就是:

    public void postData(String url,JSONObject json) {
        HttpClient httpclient = new DefaultHttpClient();
        try {
              HttpPost post = createPostForJSONObject(json, url);
              HttpResponse response = httpClient.execute(post);
              // DO YOUR THING
        } catch Exception {
             // HANDLE EXCEPTION
        }
    }
    

    【讨论】:

    • 感谢您的快速回复,我马上去测试一下
    • 我遇到了一个小问题。如何从我的代码中调用这个函数 requestNewVideoMessageWithThreadId?
    • 这里没有发送httppost的方法
    • 发送邮件只需致电defaultHttpClent.execute(request)
    • 我需要发送那个吗?在新函数中或正好在:"return se;"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多