-1

This is a question regarding making web requests using Http verbs other than GET,POST,DELETE,PUT etc in the Android platform. I can perfectly do a post in my requests using HttpPost and even attach a JSON string to it.However if I have a custom Http verb , Android does not support it directly via existing api's,though I could do that in iOS using setRequestMethod.I did come across HttpURLConnection class that lets me set the request method,however it limits me to POST,GET,DELETE,PUT and throws up an error if any other Http verb is used. I understand that classes like HttpPost,HttpGet,HttpPut etc extend HttpEntityEnclosingRequest class which in turn extends HttpRequestBase and that my solution would be around tweaking these classes and eventually attaching a JSON string(just like we attach JSON strings to a HttpPost object).

I just can't figure out how to do it.Kindly help.

4

2 回答 2

0

现在,感谢一位专家同事,这就是答案。一个需要子类HttpEntityEnclosingRequestBase

转到此链接以了解实现。

public class HttpEnroll extends HttpEntityEnclosingRequestBase {

  public final static String METHOD_NAME = "ENROLL";

  public HttpEnroll() {
    super();
  }

  public HttpEnroll(final URI uri) {
    super();
    setURI(uri);
  }

  /**
   * @throws IllegalArgumentException if the uri is invalid.
   */
  public HttpEnroll(final String uri) {
    super();
    setURI(URI.create(uri));
  }

  @Override
  public String getMethod() {
    return METHOD_NAME;
  }

}

然后HttpEnrollHttpPost.

关于附加 JSON,很多人已经回答了这个问题。

于 2013-04-17T09:30:15.753 回答
-1
JSONArray uploadArray = new JSONArray();
String URL = "http://your -url";
                String upString=uploadArray.toString();
                upString.replace("\\", "");
                Log.e("Upload Request", upString);
                String strinObjRecv = HttpClientNew.SendHttpPost(URL,
                        upString);

HttpClientNew.java

public class HttpClientNew {
    private static final String TAG = "HttpClient";

    public static String SendHttpPost(String URL, String  jsonObjSendString) {

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

            StringEntity se;
            se = new StringEntity(jsonObjSendString);
            Log.e("wtf1",EntityUtils.toString(se,HTTP.UTF_8));

            se = new StringEntity(jsonObjSendString,HTTP.UTF_8);
            Log.e("wtf2",EntityUtils.toString(se,HTTP.UTF_8));

            // Set HTTP parameters
            httpPostRequest.setEntity(se);
            httpPostRequest.setHeader("Accept", "application/json");
            httpPostRequest.setHeader("Content-type", "application/json");
            //httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

            long t = System.currentTimeMillis();
            HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
            Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");

            // Get hold of the response entity (-> the data):
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                // Read the content stream
                InputStream instream = entity.getContent();
                Header contentEncoding = response.getFirstHeader("Content-Encoding");
                if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                    instream = new GZIPInputStream(instream);
                }

                // convert content stream to a String
                String resultString= convertStreamToString(instream);
                instream.close();
                //resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"


                Log.i(TAG,"<Respose>\n"+resultString+"\n</JSONObject>\n"+response.getStatusLine().getStatusCode());

                return resultString;
            } 

        }
        catch (Exception e)
        {
            // More about HTTP exception handling in another tutorial.
            // For now we just print the stack trace.
            e.printStackTrace();
        }
        return null;
    }


    private static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

}
于 2013-04-09T12:23:01.537 回答