不调用asyncktask的doInbackground

时间:2016-05-25 09:22:30

标签: java android android-asynctask

点击按钮时会执行此网址

new HttpAsyncTasks().execute("http://www.demo.com/xyz");

这是上述执行的asynctask

private class HttpAsyncTasks extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {

        return POSTS(urls[0]);

    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), "successfull!", Toast.LENGTH_LONG).show();
        //call main activity activity upon successful registration


        Intent callMain = new Intent(getApplicationContext(),
                MainActivity.class);
        startActivity(callMain);

   }
}

上面的doInbackground永远不会被执行,但是onPostExecute方法会执行。

这是在doInbackground中调用的POSTS方法

public  String POSTS(String url){

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

           // 1. create HttpClient
           HttpClient httpclient = new DefaultHttpClient();

           // 2. make POST request to the given URL
           HttpPost httpPost = new HttpPost(url);

           String json = "";


           // 3. build jsonObject
           JSONObject jsonObject = new JSONObject();
           jsonObject.accumulate("xyz", "xyz");
           jsonObject.accumulate("amount", "800");
           jsonObject.accumulate("demo", "demo");
           jsonObject.accumulate("demo2", demo2);

           // 4. convert JSONObject to JSON to String
           json = jsonObject.toString();

           // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
           // ObjectMapper mapper = new ObjectMapper();
           // json = mapper.writeValueAsString(person); 

           // 5. set json to StringEntity
           StringEntity se = new StringEntity(json);

           // 6. set httpPost Entity
           httpPost.setEntity(se);

           // 7. Set some headers to inform server about the type of the content   
           httpPost.setHeader("Accept", "application/json");
           httpPost.setHeader("Content-type", "application/json");

           // 8. Execute POST request to the given URL
           HttpResponse httpResponse = httpclient.execute(httpPost);

           // 9. receive response as inputStream
           inputStream = httpResponse.getEntity().getContent();

           // 10. convert inputstream to string
           if(inputStream != null)
               result = convertInputStreamToString(inputStream);
           else
               result = "Did not work!";

       } catch (Exception e) {
           Log.d("InputStream", e.getLocalizedMessage());
       }

       // 11. return result
       return result;
   }

请问可能出错?

2 个答案:

答案 0 :(得分:0)

你能试试吗?它检查你的POSTS方法。 如果toast显示空行,则在WebProcess上的POSTS方法中出现错误。

private class HttpAsyncTasks extends AsyncTask<String, Void, String> {
    private String myResult="check";
    @Override
    protected String doInBackground(String... urls) {
        try
        {
            myResult = POSTS(urls[0]);

        }
        catch(Exception e)
        {
            return e;
        }
        return myResult;
    }
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();


        /* other codes */
   }
}

答案 1 :(得分:0)

您的POSTS(urls [0])调用必须提供异常,并且当它执行时,将跳过其余代码并绕过您的Toast。尝试删除可以在logcat中找到它的异常。