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でリクエストを介してJSONオブジェクトを送信するには?

于 2013-08-15T09:41:39.033 に答える