gpt4 book ai didi

java - PUT方法到服务器java

转载 作者:行者123 更新时间:2023-11-30 02:45:54 24 4
gpt4 key购买 nike

我曾尝试使用 PUT 方法向服务器发送消息,但它不起作用。服务器回复:服务器在处理请求时遇到错误。查看服务器日志...

String result = null;
InputStream inputStream = null;
try {

HttpClient httpclient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
httpPut.setHeader("Content-type", "application/json");

Log.v("Execute: ", httpPut.getRequestLine().toString());

HttpResponse httpResponse = httpclient.execute(httpPut);
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
Log.v("POST CM: ", "result\n" + result);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;

最佳答案

这是我的 HttpPut 它的工作原理,通常用于更新 web 的数据,你的 put 没有 Entity 来更新一些东西,如果你想获得输入流通常使用 HttpGet 。这是我的想法。

public static synchronized void API(String URL, JSONObject uploadata, Context context)
{
try {
URL = URL + "/" + uploadata.get("id") + "?";
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
uploadata.put("token", NetworkUtilities.Get_token(null, context));
HttpClient httpClient = new DefaultHttpClient(httpParameters);
LOG.d("DEYU", "" + 1);
HttpPut put = new HttpPut(URL);
LOG.d("DEYU", "" + 2);
put.addHeader(new BasicHeader("content-type", "application/json"));
put.setEntity(new StringEntity(uploadata.toString(), HTTP.UTF_8));
LOG.d("DEYU", "" + 3);
HttpResponse resp = httpClient.execute(put);
LOG.d("DEYU", "" + 4);
String state = String.valueOf(resp.getStatusLine().getStatusCode());
LOG.d("DEYU", "!" + state);
if (state.equals("200")) {
reSetDirty(uploadata.getString("user_id"), context);
}
} catch (Exception e) {
LOG.d("DEYU ", "Put error" + e);
} finally {
LOG.d("finally", "finally");
}
}

关于java - PUT方法到服务器java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25037914/

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