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

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);
 }
}


拉菲0
浏览 6010回答 2
2回答

alionSSS

= =不会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; }【另外】现在多数项目都用第三方框架了,你咋还用这个过时的方法?

蜂之谷

HttpPost
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android
Java