为了账号安全,请及时绑定邮箱和手机立即绑定

get请求方式怎么改成post请求方式,网站api接入改了所以get不能再用了

get请求方式怎么改成post请求方式,网站api接入改了所以get不能再用了

拉菲0 2016-12-17 19:43:07
package com.wulianwang.liaotian;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import android.os.AsyncTask;public class HttpData extends AsyncTask<String, Void, String>{ private HttpClient mHttpClient; private HttpGet mHttpGet; private HttpResponse mHttpResponse; private HttpEntity mHttpEntity; private InputStream in; private HttpGetDataListener listener;  private String url; public HttpData(String url,HttpGetDataListener listener) {  this.url = url;  this.listener = listener; }  @Override protected String doInBackground(String... params) {  try {   mHttpClient = new DefaultHttpClient();   mHttpGet = new HttpGet(url);   mHttpResponse = mHttpClient.execute(mHttpGet);   mHttpEntity = mHttpResponse.getEntity();   in = mHttpEntity.getContent();   BufferedReader br = new BufferedReader(new InputStreamReader(in));   String line = null;   StringBuffer sb = new StringBuffer();   while ((line = br.readLine()) != null) {    sb.append(line);   }   return sb.toString();  } catch (Exception e) {  }    return null; } @Override protected void onPostExecute(String result) {  listener.getDataUrl(result);  super.onPostExecute(result); }}
查看完整描述

2 回答

?
alionSSS

TA贡献57条经验 获得超82个赞

= =不会http基础请求吗?

post请求的代码内容大致如下:

/** post请求,获取返回字符串内容 */
public static HttpResult post(String url, byte[] bytes) {
   HttpPost httpPost = new HttpPost(url);
   ByteArrayEntity byteArrayEntity = new ByteArrayEntity(bytes);
   httpPost.setEntity(byteArrayEntity);
   return execute(url, httpPost);
}

/** 执行网络访问 */
private static HttpResult execute(String url, HttpRequestBase requestBase) {
   boolean isHttps = url.startsWith("https://");//判断是否需要采用https
   AbstractHttpClient httpClient = HttpClientFactory.create(isHttps);
   HttpContext httpContext = new SyncBasicHttpContext(new BasicHttpContext());
   HttpRequestRetryHandler retryHandler = httpClient.getHttpRequestRetryHandler();//获取重试机制
   int retryCount = 0;
   boolean retry = true;
   while (retry) {
      try {
         HttpResponse response = httpClient.execute(requestBase, httpContext);//访问网络
         if (response != null) {
            return new HttpResult(response, httpClient, requestBase);//需要你自己去封装返回的结果
         }
      } catch (Exception e) {
         IOException ioException = new IOException(e.getMessage());
         retry = retryHandler.retryRequest(ioException, ++retryCount, httpContext);//把错误异常交给重试机制,以判断是否需要采取从事
         LogUtils.e(e);//这个你可以直接改成Log.e()
      }
   }
   return null;
}

【另外】现在多数项目都用第三方框架了,你咋还用这个过时的方法?

查看完整回答
1 反对 回复 2016-12-17
?
蜂之谷

TA贡献564条经验 获得超863个赞

HttpPost

查看完整回答
反对 回复 2016-12-17
  • 2 回答
  • 0 关注
  • 6009 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信