0

I call a web service of real estate web site and for that i built my own method's and all.. I develop a execute method in which i will set my url as per my requirement as below :

 public void Execute(RequestMethod method) throws Exception
    {
        switch(method) {
            case GET:
            {
                //add parameters
                String combinedParams = "";
                if(!params.isEmpty()){
                    combinedParams += "?";
                    for(NameValuePair p : params)
                    {
                        String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");
                        if(combinedParams.length() > 1)
                        {
                            combinedParams  +=  "&" + paramString;
                        }
                        else
                        {
                            combinedParams += paramString;
                        }
                    }
                }

                HttpGet request = new HttpGet(url + combinedParams);

                //add headers
                for(NameValuePair h : headers)
                {
                    request.addHeader(h.getName(), h.getValue());
                }

                **executeRequest(request, url);** // This throws an exception
                break;
            }
            case POST:
            {
                HttpPost request = new HttpPost(url);

                //add headers
                for(NameValuePair h : headers)
                {
                    request.addHeader(h.getName(), h.getValue());
                }

                if(!params.isEmpty()){
                    request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
                }

                executeRequest(request, url);
                break;
            }
        }
    }

In get case , i send the request using httpget with my desired url but at that time network on main thread exception is generated.

RobinHood
  • 10,897
  • 4
  • 48
  • 97
Jalp
  • 67
  • 3
  • 8

3 Answers3

2

in your AndroidManifest you can also use the following to skip the error:

<uses-sdk android:minSdkVersion="8" 
        android:targetSdkVersion="8" />
jubei
  • 21
  • 2
  • In my case, I was testing some old apps that another person made and I could not understand why some worked and others didnt, considering that all of them use the same library. This explained the issue. Thanks! – AlvaroSantisteban Mar 12 '14 at 13:49
1

Change your code to below one i.e call execute this way

new Thread()
{
    @Override
    public void run()
    {
        Execute();
    }
}.start();
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
0

You have to use a Thread or AsyncTask, this post explains a lot about this error.

Community
  • 1
  • 1
Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30