【问题标题】:How to use HttpGet() and HttpPost() method to send and to get data from server in android API 22?如何使用 HttpGet() 和 HttpPost() 方法在 android API 22 中从服务器发送和获取数据?
【发布时间】:2015-08-04 02:13:58
【问题描述】:

我想使用 HTTP 类的 get 和 post 方法在服务器上获取和上传数据。我已经升级了我的 SDK 并在 android API 22 上工作,但由于 org.apache.http 类在 API 22 中已被弃用。下面是 API 22 之前使用的代码。

InputStream in;
StringBuilder s = null;
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 1000*10); // 10 seconds Timeout
HttpResponse response;
String service_url = context.getResources().getString(R.string.url);

HttpGet get = new HttpGet(service_url);
response=client.execute(get);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
   in = response.getEntity().getContent();
   BufferedReader reader = new BufferedReader(new InputStreamReader(in));
   String sResponse;
   s = new StringBuilder();
   while ((sResponse = reader.readLine()) != null) {
       s = s.append(sResponse);
   }
   JSONObject jsonObject = new JSONObject(s.toString());
}

【问题讨论】:

标签: android


【解决方案1】:

取自here

URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

conn.setRequestMethod("POST");


List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("firstParam", paramValue1));
params.add(new BasicNameValuePair("secondParam", paramValue2));
params.add(new BasicNameValuePair("thirdParam", paramValue3));

希望对您有所帮助:)

【讨论】:

  • NameValuePair 在 API 22 中消失了
猜你喜欢
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
  • 2023-03-15
  • 2014-10-28
相关资源
最近更新 更多