0

我有一个小问题,我必须将所有联系人从电话发送到服务器。此代码在发送具有 20 或 40 个联系人的 JSONObject 时有效,但当我发送 400 个联系人时返回此错误

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
 <title>400 Bad Request</title>
 </head><body>
 <h1>Bad Request</h1>
 <p>Your browser sent a request that this server could not understand.<br />
Size of a request header field exceeds server limit.<br />
  <pre>
  json
 </pre>/n</p>
  <hr>
  <address>Apache Server at default Port 80</address>
 </body></html>

这是代码的工作原理

public String postData(String url,JSONObject obj) {
        // Create a new HttpClient and Post Header

        HttpParams myParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(myParams, 10000);
        HttpConnectionParams.setSoTimeout(myParams, 10000);
        HttpClient httpclient = new DefaultHttpClient(myParams);
        String json=obj.toString();

        try {

            HttpPost httppost = new HttpPost(url.toString());
            httppost.setHeader("json", obj.toString());

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

            HttpResponse response = httpclient.execute(httppost);
            String temp = EntityUtils.toString(response.getEntity());
            Log.i("post_data2 - popolo", temp);

             return temp;

        } 

感谢您的回复

/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * **

我自己解决了。我希望它可以帮助某人。我为此更改了函数 postData。

public void postinsert(String url,String unico_id) {


        try{
            unico_id = URLEncoder.encode("unique_id", "UTF-8") + "=" + URLEncoder.encode(unico_id, "UTF-8");
            URL url1 =new URL(url);
              URLConnection con = (URLConnection) url1.openConnection();
              con.setDoOutput(true);
              OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
              wr.write(unico_id);
              wr.flush();
              BufferedReader recv = new BufferedReader(new InputStreamReader(con.getInputStream()));
              //Los mostramos por pantalla
               s = recv.readLine();
              while(s!=null){
                      System.out.println(s);
                      s=recv.readLine();
                    }
           }catch (Exception e){
               System.out.println(e.getMessage());
           }

        }
4

0 回答 0