Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中使用自定义Http谓词并附加JSON字符串进行web请求_Java_Android_Json_Web Services_Rest - Fatal编程技术网

Java 如何在Android中使用自定义Http谓词并附加JSON字符串进行web请求

Java 如何在Android中使用自定义Http谓词并附加JSON字符串进行web请求,java,android,json,web-services,rest,Java,Android,Json,Web Services,Rest,这是一个关于在Android平台上使用Http动词而不是GET、POST、DELETE、PUT等发出web请求的问题。我可以使用HttpPost在我的请求中完美地完成一个post,甚至可以将一个JSON字符串附加到它。但是,如果我有一个自定义的Http谓词,Android不会通过现有的api直接支持它,尽管我可以在iOS中使用setRequestMethod来完成。我确实遇到了允许我设置请求方法的HttpURLConnection类,但是它限制了我进行post,如果使用了任何其他Http谓词,则

这是一个关于在Android平台上使用Http动词而不是GET、POST、DELETE、PUT等发出web请求的问题。我可以使用HttpPost在我的请求中完美地完成一个post,甚至可以将一个JSON字符串附加到它。但是,如果我有一个自定义的Http谓词,Android不会通过现有的api直接支持它,尽管我可以在iOS中使用setRequestMethod来完成。我确实遇到了允许我设置请求方法的HttpURLConnection类,但是它限制了我进行post,如果使用了任何其他Http谓词,则GET、DELETE、PUT和抛出错误。我知道像HttpPost、HttpGet、HttpPut等类扩展了HttpEntityEnclosingRequest类,而HttpEntityEnclosingRequest类又扩展了HttpRequestBase,我的解决方案是调整这些类并最终附加JSON字符串(就像我们将JSON字符串附加到HttpPost对象一样)

我就是不知道怎么做。请帮忙

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

公共类HttpClientNew{
私有静态最终字符串TAG=“HttpClient”;
公共静态字符串SendHttpPost(字符串URL、字符串jsonObjSendString){
试一试{
DefaultHttpClient httpclient=新的DefaultHttpClient();
HttpPost httpPostRequest=新的HttpPost(URL);
Stringse;
se=新的StringEntity(jsonObjSendString);
Log.e(“wtf1”,EntityUtils.toString(se,HTTP.UTF_8));
se=新的StringEntity(jsonObjSendString,HTTP.UTF_8);
Log.e(“wtf2”,EntityUtils.toString(se,HTTP.UTF_8));
//设置HTTP参数
httpPostRequest.setEntity(se);
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
//httpPostRequest.setHeader(“接受编码”、“gzip”);//仅当要使用gzip压缩时才设置此参数
long t=System.currentTimeMillis();
HttpResponse response=(HttpResponse)httpclient.execute(httpPostRequest);
Log.i(标记,“HTTPResponse在[”+(System.currentTimeMillis()-t)+“ms]”中接收;
//获取响应实体(->数据):
HttpEntity=response.getEntity();
如果(实体!=null){
//阅读内容流
InputStream instream=entity.getContent();
Header contentEncoding=response.getFirstHeader(“内容编码”);
if(contentEncoding!=null&&contentEncoding.getValue().equalsIgnoreCase(“gzip”)){
流入=新的GZIPInputStream(流入);
}
//将内容流转换为字符串
字符串结果字符串=convertStreamToString(流内);
流内关闭();
//resultString=resultString.substring(1,resultString.length()-1);//删除包装“[”和“]”
Log.i(标记“\n”+resultString+”\n\n“+response.getStatusLine().getStatusCode());
返回结果字符串;
} 
}
捕获(例外e)
{
//有关HTTP异常处理的更多信息,请参见另一教程。
//现在我们只打印堆栈跟踪。
e、 printStackTrace();
}
返回null;
}
私有静态字符串convertStreamToString(InputStream为){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
使某人返回字符串();
}
}

现在,多亏了一位专家同事,以下是答案。需要对HttpEntityEnclosingRequestBase进行子类
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;
  }

}
然后使用
HttpEnroll
,就像使用
HttpPost
一样


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

谢谢你的努力。你的代码让我可以做一个Http Post(我知道如何编码),但没有回答我的问题。我的web服务不接受Http动词的Post。上面的代码使用Post(通过使用HttpPost httpPostRequest=new HttpPost(URL);)例如,我需要像HttpEnroll httpEnrollRequest=new HttpEnroll(URL)这样的东西,其中Enroll是一个自定义Http动词,Android自然没有它。
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;
  }

}