1

如何在 Android 中发送 POST 方法,一个参数需要是 String 和另一个 JSON 对象?

字符串参数是“年龄”

            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

利用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 回答
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 回答