我想从我的Android应用程序发送数据到servlet。 我试图在我的应用程序中使用它:
private boolean postData(String s) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(s); // s is my url
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "1003"));
nameValuePairs.add(new BasicNameValuePair("name", "Anil"));
nameValuePairs.add(new BasicNameValuePair("loc", "Kalyan"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
// TODO Auto-generated catch block
return false;
}
return true;
}
我是这种方法,但无法导入这些文件:
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
如何在android studio中影响这些文件?或者还有其他方法吗? Thanx提前
答案 0 :(得分:0)
Android 6.0版本删除了对Apache HTTP客户端的支持。如果您的应用使用此客户端并定位到Android 2.3(API级别9)或更高版本,请改用HttpURLConnection类
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000);
conn.setReadTimeout(2000);
conn.setDoOutput(true);
byte[] bypes = params.toString().getBytes();
conn.getOutputStream().write(bypes);
InputStream inStream = conn.getInputStream();