gpt4 book ai didi

android - HTTPRequest:setEntity 不起作用

转载 作者:行者123 更新时间:2023-11-29 00:17:40 28 4
gpt4 key购买 nike

在我的 android 应用程序中,我试图通过后请求从我的服务器获取(在异步任务中)一个 json 字符串:

        ...

HttpClient httpClient = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);


CookieSyncManager.createInstance(getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();
String cookies = cookieManager.getCookie("www.myExample.com");

HttpPost post = new HttpPost("www.myExample.com/api/");

post.setHeader("Referer", "https://www.myExample.com/");
post.setHeader("X-Requested-With", "XMLHttpRequest");
post.setHeader("Cookie", cookies);

Pattern pattern = Pattern.compile("csrftoken=(.*?);");
Matcher matcher = pattern.matcher(cookies);
String csrftoken = "";
if (matcher.find())
{
csrftoken = matcher.group(1);
}

post.setHeader("X-CSRFToken", csrftoken);

>> List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("output_format", "json"));
nameValuePairs.add(new BasicNameValuePair("plain", "true"));


try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8")); <<

HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(content));

String result, line = reader.readLine();
result = line;

while((line = reader.readLine()) != null) {
result += line;
}

if (result != null) {
try {
JSONObject jObj = new JSONObject(result);

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

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
Log.d("result", "not");
Log.d("result", e.toString());
e.printStackTrace();
}

...

在我的例子中,实体的配置似乎失败了,我总是得到结果,就好像我会在没有实体的情况下发送请求,并带有他重要的 nameValuePairs。我用 >> <<.

标记了相关行

最佳答案

将您的代码更新为要发布的代码段

   public String doInBackground(....){  

DefaultHttpClient httpClient = new DefaultHttpClient();
String jsonString = "{\"example\": \"go\"}";
String url = "www.myExample.com/api/";
HttpPost httpReq = new HttpPost(url );
httpReq.setHeader("Accept", "application/json");
httpReq.setHeader("Content-type", "application/json");
StringEntity se = new StringEntity(jsonString, "UTF-8");
httpReq.setEntity(se);
Log.e("Invoking Post API", ((HttpPost) httpReq).getURI().toString());

HttpResponse response = httpClient.execute(((HttpPost) httpReq));
String result = EntityUtils.toString(response.getEntity());

return result;
}

和这个要获取的 fragment

公共(public)字符串 doInBackground(....){

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpReq = new HttpGet("www.myExample.com/api/");
HttpResponse response = httpClient.execute(httpReq);
String responseText = EntityUtils.toString(response.getEntity());
result = responseText;
return result;
}

关于android - HTTPRequest:setEntity 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25481759/

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