进行HTTP调用时显示以下堆栈跟踪

时间:2014-04-09 07:46:54

标签: java android json http

我正在使用以下代码行从服务器获取HttpResponce,但在运行代码时,我在logcat中得到了跟踪堆栈跟踪。有什么线索的原因?

我使用的代码

new DeleteDirectoryLoaderTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

class DeleteDirectoryLoaderTask extends AsyncTask<URL, Integer, Long>
    {
        DeleteDirectoryLoaderTask()
        {

        }

        @Override
        protected void onPreExecute()
        {   

        }

        @Override
        protected Long doInBackground(URL... urls)
        {           


            try 
            {
                HttpResponse responceFromServer = m_HttpResponceInstance.makeRequest("post", HTTP_URI + "/" + "signup", registrationHeader, jsonMain);  
            } 
            catch (Exception e) 
            {                   
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... progress)
        {

        }

        @Override
        protected void onPostExecute(Long result)
        {           

        }
    }   

public HttpResponse makeRequest(String httpMethod, String url, Map<String, String> Header, JSONObject jsonObject) throws Exception 
    {
        HttpResponse responce = null;

        if(httpMethod.equals("post"))
        {
            DefaultHttpClient httpclient = new DefaultHttpClient();

            //url with the post data
            HttpPost httpost = new HttpPost(url);

            //passes the results to a string builder/entity
            StringEntity se = new StringEntity(jsonObject.toString());

            //sets the post request as the resulting string
            httpost.setEntity(se);

            for ( String key : Header.keySet() )
            {
                //sets a request header so the page receving the request
                //will know what to do with it
                httpost.setHeader(key, Header.get(key));
            }

            responce = httpclient.execute(httpost); 
            System.out.println(responce);
        }

        return  responce;
    }

log cat中的堆栈跟踪

06-07 13:24:14.007: W/System.err(1857): android.os.NetworkOnMainThreadException
06-07 13:24:14.017: W/System.err(1857):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
06-07 13:24:14.022: W/System.err(1857):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-07 13:24:14.022: W/System.err(1857):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-07 13:24:14.022: W/System.err(1857):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-07 13:24:14.027: W/System.err(1857):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-07 13:24:14.027: W/System.err(1857):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-07 13:24:14.027: W/System.err(1857):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-07 13:24:14.027: W/System.err(1857):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-07 13:24:14.032: W/System.err(1857):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
06-07 13:24:14.032: W/System.err(1857):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
06-07 13:24:14.032: W/System.err(1857):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-07 13:24:14.032: W/System.err(1857):     at in.plackal.lovecyclesfree.HttpResponceManager.makeRequest(HttpResponceManager.java:54)
06-07 13:24:14.037: W/System.err(1857):     at in.plackal.lovecyclesfree.RegisterAccountActivity.onClick(RegisterAccountActivity.java:161)
06-07 13:24:14.037: W/System.err(1857):     at android.view.View.performClick(View.java:4261)
06-07 13:24:14.037: W/System.err(1857):     at android.view.View$PerformClick.run(View.java:17356)
06-07 13:24:14.037: W/System.err(1857):     at android.os.Handler.handleCallback(Handler.java:615)
06-07 13:24:14.037: W/System.err(1857):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 13:24:14.042: W/System.err(1857):     at android.os.Looper.loop(Looper.java:137)
06-07 13:24:14.042: W/System.err(1857):     at android.app.ActivityThread.main(ActivityThread.java:4921)
06-07 13:24:14.042: W/System.err(1857):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 13:24:14.042: W/System.err(1857):     at java.lang.reflect.Method.invoke(Method.java:511)
06-07 13:24:14.047: W/System.err(1857):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-07 13:24:14.047: W/System.err(1857):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-07 13:24:14.047: W/System.err(1857):     at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:1)

这必须在asynctask中完成。 (http://developer.android.com/reference/android/os/AsyncTask.html) 将代码放在doInBackground

答案 1 :(得分:0)

Async task亲爱的

中完成这项工作

当应用程序尝试在其主线程上执行网络操作时,抛出此异常。在AsyncTask中运行您的代码:(http://developer.android.com/reference/android/os/AsyncTask.html

如何执行任务:

new RetrieveFeedTask().execute(urlToRssFeed);

不要忘记将其添加到AndroidManifest.xml文件中:

<uses-permission android:name="android.permission.INTERNET"/>

答案 2 :(得分:0)

请从后台线程调用此方法。即使用AsyncTask。然后从doInBackground()

中调用此方法

答案 3 :(得分:0)

您正在主线程上运行任务,查看堆栈跟踪的第12行。

  

android.os.NetworkOnMainThreadException

根据这个答案:https://stackoverflow.com/a/19323642/619673

  

您无法在主线程上发出网络请求。你会得到的   你现在看到的错误。您需要使用AsyncTask或者您需要   创建一个新线程。就个人而言,我使用AsyncTask。当你使用   AsyncTask可以使用onPostExecute方法返回值   主线程。 - 布莱恩