我試圖使用這種方法,我也試圖給libriries,但一切徒勞。請幫助我。沒有任何HTTPClient庫在我的andriod studio上顯示。幫助仍然使用的是HttpClient.You可以使用HttpURLConnection類,排球等HttpClient的類現在deprecated.Also添加Internet權限,dependensies在gradle這個文件理解HttP Post方法不起作用
public String getHttpPost(String url,ContentValues) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
您是否在您的gradle文件中添加了依賴關係? – Prexx
您是否在AndroidManifeast.xml中添加了Internet權限 –
您是否想要另一種方法進行網絡調用,例如使用庫如volley或Retrofit? –