2

我正在尝试使用我尝试过此代码在我的 android 应用程序中发送一些信息

try{

    JSONObject j = new JSONObject();
    j.put("engineer", "me");

    httppost.setEntity(new UrlEncodedFormEntity(j));    
    HttpResponse response = httpclient.execute(httppost);

    /*Checking response*/
    if(response!=null)
    {   
        responseBody = EntityUtils.toString(response.getEntity());

    }
    if(responseBody.equals("ok"))
    {

        //...do something

    }
} catch(ClientProtocolException e) {

} catch (IOException e) {
    // TODO Auto-generated catch block
} catch(Exception e){
    e.printStackTrace();
}

但我认为有一些错误

 httppost.setEntity(new UrlEncodedFormEntity(j));

你能帮我解决这个问题吗

4

1 回答 1

0

您正在放置 JSON 而不是名称值对。JSON 应该是另一个键的值,或者您不要仅将 JSON 用于一个值和一个键:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("json", j.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
于 2013-07-26T18:05:19.910 回答