0

Hi i'm trying to get a json object from my MVC asp.net.

I'm getting an error

            StringBuilder url = new StringBuilder(URL);

    HttpGet get = new HttpGet (url.toString());

    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();

    if(status == 200)
    {
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);

        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(0);

        return last;
    }
    else 
    {
        Toast.makeText(Authenticate.this, "error", Toast.LENGTH_LONG);
        return null;
    }

The error:

Java.IOException.

             public class Read extends AsyncTask <String,Integer,String>
      {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try{
                json = Verify();
                return json.getString(params[0]);
            }
            catch (ClientProtocolException e)
            {               
            }
            catch (IOException e)
            {               
            }
            catch (JSONException e)
            {

            }
            return null;
        }

I am currently connecting via localhost.

The problem happen at the line: HttpResponse r = client.execute(get);

Its not executing and the application on the VM is crashing.

I have amended the code to look like this but still getting the same error:

   public JSONObject Verify(HttpResponse r) throws ClientProtocolException,                               IOException, JSONException
{
    //StringBuilder url = new StringBuilder(URL);
    //Adds the pin and IMEI.
    //url.append(b);
    //HttpGet get = new HttpGet (url.toString());

    //HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();

    if(status == 200)
    {
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);

        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(0);

        return last;
    }
    else 
    {
        Toast.makeText(Authenticate.this, "error", Toast.LENGTH_LONG);
        return null;
    }

}

    public class Read extends AsyncTask <String,Integer,String>
    {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try{
                HttpResponse r = client.execute(get);
                json = Verify(r);
                return json.getString(params[0]);
            }
            catch (ClientProtocolException e)
            {               
            }
            catch (IOException e)
            {               
            }
            catch (JSONException e)
            {

            }
            return null;
        }

The Log Cat is giving me this error: 07-10 20:53:23.578: E/ActivityThread(661): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d17eb0 that was originally bound here 07-10 20:53:23.578: E/ActivityThread(661): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d17eb0 that was originally bound here 07-10 20:53:23.578: E/ActivityThread(661): at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:969) 07-10 20:53:23.578: E/ActivityThread(661): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 07-10 20:53:23.578: E/ActivityThread(661): at android.app.ContextImpl.bindService(ContextImpl.java:1418) 07-10 20:53:23.578: E/ActivityThread(661): at android.app.ContextImpl.bindService(ContextImpl.java:1407) 07-10 20:53:23.578: E/ActivityThread(661): at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 07-10 20:53:23.578: E/ActivityThread(661): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 07-10 20:53:23.578: E/ActivityThread(661): at android.os.AsyncTask$2.call(AsyncTask.java:287) 07-10 20:53:23.578: E/ActivityThread(661): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 07-10 20:53:23.578: E/ActivityThread(661): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 07-10 20:53:23.578: E/ActivityThread(661): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 07-10 20:53:23.578: E/ActivityThread(661): at java.lang.Thread.run(Thread.java:856) 07-10 20:53:23.588: E/StrictMode(661): null 07-10 20:53:23.588: E/StrictMode(661): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d17eb0 that was originally bound here 07-10 20:53:23.588: E/StrictMode(661): at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:969) 07-10 20:53:23.588: E/StrictMode(661): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 07-10 20:53:23.588: E/StrictMode(661): at android.app.ContextImpl.bindService(ContextImpl.java:1418) 07-10 20:53:23.588: E/StrictMode(661): at android.app.ContextImpl.bindService(ContextImpl.java:1407) 07-10 20:53:23.588: E/StrictMode(661): at android.content.ContextWrapper.bindService(ContextWrapper.java:473) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 07-10 20:53:23.588: E/StrictMode(661): at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 07-10 20:53:23.588: E/StrictMode(661): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 07-10 20:53:23.588: E/StrictMode(661): at android.os.AsyncTask$2.call(AsyncTask.java:287) 07-10 20:53:23.588: E/StrictMode(661): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 07-10 20:53:23.588: E/StrictMode(661): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 07-10 20:53:23.588: E/StrictMode(661): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 07-10 20:53:23.588: E/StrictMode(661): at java.lang.Thread.run(Thread.java:856) 07-10 20:53:23.588: W/ActivityManager(291): Unbind failed: could not find connection for android.os.BinderProxy@4100de60

1 Answer 1

1

It's hard to tell without more information, but you may be running this on your UI thread which is not allowed. If this is the case you will need to use an AsynTask.

Sign up to request clarification or add additional context in comments.

6 Comments

I am running it in an AsynTask as follows public class Read extends AsyncTask <String,Integer,String> { @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub try{ json = Verify(); return json.getString(params[0]); } catch (ClientProtocolException e) { } catch (IOException e) { } catch (JSONException e) { } return null; }
Could you edit your initial question to add that stuff in? It's a bit hard to read.
I have just added it Thanks for your interest.
Ok, your network call ( HttpResponse r = client.execute(get);) needs to be inside of the async task.
Hmm, everything that is used by the response should go in the async task as well, this isn't completely necessary, but it just helps keep everything together. Not sure why you are still getting the error, could you post the logcat where it shows the errors and such? Also, as a side note, I usually don't use strings for httpGet, I prefer a URI. I doubt this is the source of the problem though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.