1

我试图得到像

StringEntity  entity;
private static final int TIMEOUT_MILLISEC = 20000;
private static final String serverurl = "http://appcodetechnology.net/meravenue/api/";

下面的函数 m 调用

  post("master_details.php");

//

public String post(String request)
    {
        String result = null;
        try
        {   
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
            HttpClient client = new DefaultHttpClient(params);
            System.out.println("Getting url: "+request);
            HttpPost post = new HttpPost(serverurl+request);

             String body  = "{\"action\":\"get_all_cities\"}";

                try {
                    //entity = new StringEntity("action=" + java.net.URLEncoder.encode(body, "utf-8"));                     
                    //entity = new StringEntity("data=" + java.net.URLEncoder.encode(body, "utf-8"));                       
                     entity = new StringEntity(java.net.URLEncoder.encode(body, "utf-8"));                          
                     entity.setContentType("application/x-www-form-urlencoded");

                } catch (UnsupportedEncodingException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
               // entity.setContentType("application/x-www-form-urlencoded");
                post.setEntity(entity);


            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            if(entity != null)
            {
                result = EntityUtils.toString(entity);
            }
            else
            {
                Log.d("Response Failed","Response from server is failed");
            }

        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return result;
    }

并得到回应

{"result":false,"message":"Invalid method name.","error_code":"406"}

我没有任何错误。

对于以下 API 我正在工作

URL : http://appcodetechnology.net/meravenue/api/master_details.php
Method : POST
Request Body : {"action":"get_all_cities"}

但没有得到回应

4

1 回答 1

1

使用它来发布一个 json 对象:

                JSONObject json = new JSONObject();
                json.put("YOUR_KEY", YOUR_VALUE);
                post.setEntity(new ByteArrayEntity(json .toString().getBytes("UTF8")));

或者只是参考此页面以查找如何发布 json:

如何使用 Android 通过 Request 发送 JSON 对象?

于 2013-08-15T09:41:39.033 回答