无法从HttpClient POST获取HttpPost响应

时间:2017-12-09 04:13:28

标签: 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-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工作室中调试它,所以我添加了控制台日志,我可以看到日志直到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";
}

这就是我在我的活动类{ - 1}}中调用它的方式 -

onCreate

这里是Logcat的详细信息 -

try
{
    CustomAsyncUtil objUtil = new CustomAsyncUtil();
    objUtil.execute();
}
catch (Exception ex)
{
    String exp = ex.getMessage();
}

0 个答案:

没有答案