gpt4 book ai didi

java - Android Studio - httpparams、httpclient 等旧命令不再有效

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:41 26 4
gpt4 key购买 nike

HttpClientHttpPost 等命令无法正常工作。谁能帮帮我?

 @Override
protected users doInBackground(Void... params){
Map<String, String> dataToSend = new HashMap<>();
dataToSend.put("x", users.x + "");
dataToSend.put("y", users.y);

URL url = new URL(SERVER_ADRESS);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpParams httpRequestPramas = new BasicHttpParams();
conn.setConnectionTimeout(httpRequestPramas, CONNECTION_TIME);
HttpConnectionParams.setSoTimeout(httpRequestPramas, CONNECTION_TIME);

HttpClient client = new DefaultHttpClient(httpRequestPramas);
HttpPost post = new HttpPost(SERVER_ADRESS + "login.php");

users returnedusers = null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);

HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if (jObject.length() == 0){
users = null;
}else{
String vorname = jObject.getString("y");
int kundennummer = jObject.getInt("x");

returnedusers = new users(users.y, users.x);

}
}catch (Exception e){
e.printStackTrace();
}

return returnedusers;
}

感谢您的帮助。

最佳答案

这样试试

URL url = new URL("http://example.sitedemo.service.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
Uri.Builder builder = new Uri.Builder().appendQueryParameter("username", "maven")
.appendQueryParameter("password", "123");
String query = builder.build().getEncodedQuery();

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();

conn.connect();

InputStream in = new BufferedInputStream(conn.getInputStream());
response = IOUtils.toString(in, "UTF-8");

引用- android.net.Uri.Builder

关于java - Android Studio - httpparams、httpclient 等旧命令不再有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31338087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com