且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从应用程序,但服务器&QUOT Android版发送HTTP POST请求,认为"它作为一个GET请求

更新时间:2022-04-28 08:24:37

使用这种方法我的朋友张贴数据到服务器

Use this method my friend for posting data to server

public  Boolean postDataWithURL(String url, String fileUrl,
            ArrayList<NameValuePair> listParamsWithValues) {
        boolean result = false;
        try {
            int TIMEOUT_MILLISEC = 100000; // = 10 seconds
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams,
                    TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
            HttpClient client = new DefaultHttpClient(httpParams);
            HttpPost request = new HttpPost(url);

            if (fileUrl.equalsIgnoreCase("")) {
                request.setEntity(new UrlEncodedFormEntity(listParamsWithValues));
            } else {
                // System.out.println("file path "+fileUrl+" with actual path "+file);
            }
            // request.setEntity(new
            // ByteArrayEntity(listParamsWithValues.toString().getBytes("UTF8")));
            HttpResponse response = client.execute(request);
            String responseString = request(response);
            // System.out.println(responseString);
            if (responseString.toLowerCase().contains("1")) {
                result = true;
            } else {
                result = false;
            }
        } catch (Exception e) {
            // Some things goes Wrong
            e.printStackTrace();
        }
        return result;
    }