[英]how do i enable httpclient in android studios?
救命! 如何在Android Studio中啟用以下httpclient? 似乎找不到NameValuePair,BasicNameValuePair,Httpclient,Httppost,並且顯然我的HTTPConnectionParam被取消了嗎? 我該如何解決?
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name",user.name));
dataToSend.add(new BasicNameValuePair("email",user.email));
dataToSend.add(new BasicNameValuePair("password",user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch (Exception e) {
e.printStackTrace();
}
我假設您可能正在使用sdk 23+,請嘗試使用URLConnection或降級為sdk 22。
最近,我不得不更改幾乎所有代碼,因為該庫已被棄用。 我相信我們從現在開始建議使用原始的Java網絡庫。
嘗試以下
try{
URL url = new URL(SERVER_ADDRESS + "Register.php");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
String postData = URLEncoder.encode("name","UTF-8")
+"="+URLEncoder.encode(user.name,"UTF-8");
postData += "&"+URLEncoder.encode("email","UTF-8")
+"="+URLEncoder.encode(user.email,"UTF-8");
postData += "&"+URLEncoder.encode("password","UTF-8")
+"="+URLEncoder.encode(user.password,"UTF-8");
OutputStreamWriter outputStreamWriter = new
OutputStreamWriter(connection.getOutputStream());
outputStreamWriter.write(postData);
outputStreamWriter.flush();
outputStreamWriter.close();
}catch(IOException e){
e.printStackTrace();
}
希望能幫助到你
BasicNameValuePair也已棄用。 使用HashMap發送鍵和值。
HashMap文檔: http : //developer.android.com/reference/java/util/HashMap.html
使用此方法以便將數據發布到“ yourFiles.php”。
public String performPostCall(String requestURL, HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}
else {
response="";
throw new HttpException(responseCode+"");
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
private String getPostDataString(Map<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
您也可以使用Google的Volley Library來完成工作。
使用庫的示例:
RequestQueue queue = Volley.newRequestQueue(activity);
StringRequest strRequest = new StringRequest(Request.Method.POST, "Your URL",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
VolleyLog.d("Home_Fragment", "Error: " + response);
Toast.makeText(activity, "Success", Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(getApplicationContext(), "Error: " + error.getMessage());
Toast.makeText(activity, error.toString(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("name", user.name);
params.put("email", user.email;
params.put("password", user.password);
return params;
}
};
queue.add(strRequest);
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.