1

AndroidでPOSTメソッドを送信するには、1つのパラメータが文字列で、別のJSONオブジェクトが必要ですか?

文字列パラメータは「age」

            JSONObject createRequest = new JSONObject();
            try {
                createRequest.put("ID", 2173);
                createRequest.put("Name", "Munja");
                createRequest.put("Address", "New York");

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


            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            //nameValuePairs.add(new BasicNameValuePair("client", createRequest.toString()));
            nameValuePairs.add(new BasicNameValuePair("age", "21"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


            StringEntity se = new StringEntity( createRequest.toString());  
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httppost.setEntity(se);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

しかし、うまく形成されていないように、うまくいかず、400を受け取ります。

4

2 に答える 2

0
public String SendHttpPost(String URL, JSONObject jsonObjSend) throws ClientProtocolException, IOException  {


            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPostRequest = new HttpPost(URL);

            jsonObjSend.length();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(jsonObjSend.length());
            nameValuePairs.add(new BasicNameValuePair("data", jsonObjSend.toString()));

            UrlEncodedFormEntity en=new UrlEncodedFormEntity(nameValuePairs);
            en.getContent();
            httpPostRequest.getParams().setParameter("http.socket.timeout", new Integer(600000));
            httpPostRequest.setEntity(en);
            long t = System.currentTimeMillis();
            HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);

            responses = convertEntityToString(response.getEntity(), "UTF-8");

        return responses;
    }
于 2013-09-17T09:23:50.637 に答える
0

使用するhttpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

コンテンツ タイプを削除します。

DefaultHttpClient httpclient = null;
    boolean success = false;

    try {           

        httpclient = new DefaultHttpClient();

        String url = mDataSupplier.getURL();


        HttpGet httpget = new HttpGet(url);

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            entity.consumeContent();
        }


        HttpPost httpost = new HttpPost(url);

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("client", createRequest.toString()));


        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));


        response = httpclient.execute(httpost);

        entity = response.getEntity();

        if (entity != null) {

            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            if(statusCode != 200){
                mResErr.onErrorResponse(statusCode);                    
            }

            InputStream is = entity.getContent();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ( (line = br.readLine()) != null) {
                //System.out.println("Data Sender: " +  line);

                if(!line.trim().equals("")){

                }           

            }
            is.close();

            //entity.consumeContent();
        } else {

        }
        success = true;         

    } catch (Exception e) {
        mResErr.onErrorResponse(e);
        e.getStackTrace();
    }

    if (httpclient != null) {
        // resource cleanup
        httpclient.getConnectionManager().shutdown();
    }
于 2013-09-17T09:09:33.270 に答える