Java 无法从HttpClient POST获取HttpPost响应
我正在异步发送Http Post请求Java 无法从HttpClient POST获取HttpPost响应,java,android,android-asynctask,Java,Android,Android Asynctask,我正在异步发送Http Post请求 org.apache.http.client.HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(HOST); httpPost.addHeader("Authorization", hashval ); httpPost.addHeader("x-ms-date", dateString); httpPost.addHeader("x-ms-v
org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(HOST);
httpPost.addHeader("Authorization", hashval );
httpPost.addHeader("x-ms-date", dateString);
httpPost.addHeader("x-ms-version", restServiceVersion);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
Gson gson = new Gson();
String jsonValue = gson.toJson(objToDoItem);
System.out.println(jsonValue);
StringEntity requestEntity = new StringEntity(jsonValue);
requestEntity.setContentType("application/json");
httpPost.setEntity(requestEntity);
System.out.println("Before request"); //upto here I can see logs in console
HttpEntity responseEntity = httpClient.execute(httpPost).getEntity(); //no response
System.out.println(EntityUtils.toString(responseEntity));
由于某些原因,我无法在Android studio中调试它,因此我添加了控制台日志,可以看到日志直到-httpClient.execute
运行。在这一行之后没有回应。我已将其包含在try catch
中,但没有捕获异常。
有人能看一下吗?如果我能提供更多细节,请告诉我
编辑
下面是异步任务的完整代码-
private class CustomAsyncUtil extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String[] params) {
System.out.println("Inside dobackground");
//if(android.os.Debug.isDebuggerConnected())
// android.os.Debug.waitForDebugger();
//CreateDocument();
System.out.println("Inside dobackground after debug point");
try
{
TodoItem objToDoItem = new TodoItem();
objToDoItem.id = UUID.randomUUID().toString();
objToDoItem.category = "test android";
objToDoItem.complete = true;
objToDoItem.name = "souvik ghosh";
String HOST = _host;
String MASTER_KEY = _key;
String restServiceVersion = "2017-02-22";
String verb = "post";
String resourceType = "docs";
String resourceId = "dbs/ToDoList/colls/Items";
SimpleDateFormat dateVal = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
System.out.println("Step 1");
String dateString = org.apache.http.impl.cookie.DateUtils.formatDate(new Date(System.currentTimeMillis()));
String gmtIndex = "GMT";
int index = dateString.indexOf(gmtIndex);
dateString = dateString.substring(0, index + 3).toLowerCase();
String payLoad = verb +"\n" + resourceType + "\n" + resourceId + "\n" + dateString + "\n\n";
System.out.println(payLoad);
String secretAccessKey = MASTER_KEY;
String data = payLoad;
byte[] secretKey = Base64.decode(secretAccessKey, Base64.DEFAULT);
SecretKeySpec signingKey = new SecretKeySpec(secretKey, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
byte[] bytes = data.getBytes("UTF-8");
byte[] rawHmac = mac.doFinal(bytes);
String hashval = Base64.encodeToString(rawHmac, Base64.DEFAULT);
org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(HOST);
httpPost.addHeader("Authorization", hashval );
httpPost.addHeader("x-ms-date", dateString);
httpPost.addHeader("x-ms-version", restServiceVersion);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
Gson gson = new Gson();
String jsonValue = gson.toJson(objToDoItem);
System.out.println(jsonValue);
StringEntity requestEntity = new StringEntity(jsonValue);
requestEntity.setContentType("application/json");
httpPost.setEntity(requestEntity);
System.out.println("Before request");
HttpEntity responseEntity = httpClient.execute(httpPost).getEntity();
System.out.println(EntityUtils.toString(responseEntity));
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println("Inside catch block");
System.out.println("Exception details: " + ex.getMessage());
System.out.println("Stack trace: " + ex.getStackTrace());
}
System.out.println("Step end");
return "some message";
}
这是日志的详细信息-
I/System.out: Inside dobackground
I/System.out: Inside dobackground after debug point
I/System.out: Step 1
I/System.out: post
I/System.out: docs
I/System.out: dbs/ToDoList/colls/Items
I/System.out: sat, 09 dec 2017 04:26:58 gmt
I/System.out: {"category":"test android","complete":true,"id":"0846439f-4951-4475-9ea5-d7937c32d479","name":"souvik ghosh"}
I/System.out: Before request
Disconnected from the target VM, address: 'localhost:8621', transport: 'socket'
值得一提的是,ApacheHTTP从Android 23开始就被弃用,取而代之的是HTTPUrlConnection,但是您可以使用Volley,正如Android文档所描述的那样,作为更新建议,用于网络。“它很快也很容易集成。”cricket_007是的,我看到了。但无论如何,我想这应该行得通?应该行,但是你能展示一下你的异步任务吗?@cricket_007我会在这里发布详细信息,以防我找到一些解决方案。谢谢
I/System.out: Inside dobackground
I/System.out: Inside dobackground after debug point
I/System.out: Step 1
I/System.out: post
I/System.out: docs
I/System.out: dbs/ToDoList/colls/Items
I/System.out: sat, 09 dec 2017 04:26:58 gmt
I/System.out: {"category":"test android","complete":true,"id":"0846439f-4951-4475-9ea5-d7937c32d479","name":"souvik ghosh"}
I/System.out: Before request
Disconnected from the target VM, address: 'localhost:8621', transport: 'socket'