0

I have an asyncTask like this one:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.util.Log;

public class OfficeJSONParser extends AsyncTask<String, String, JSONObject> {

    private ProgressDialog progressDialog;
    InputStream inputStream = null;
    String result = "";
    Context c;
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    String url;

    public OfficeJSONParser(Context c, String url) {
        this.c = c;
        this.url = url;
    }

    protected void onPreExecute() {
        progressDialog = new ProgressDialog(c);
        progressDialog.setMessage("Downloading your data...");
        progressDialog.show();
        progressDialog.setOnCancelListener(new OnCancelListener() {
            public void onCancel(DialogInterface arg0) {
                OfficeJSONParser.this.cancel(true);
            }
        });
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        return getJSONFromUrl(url);
    }

    protected void onPostExecute(JSONObject jObj) {
        JSONObject json = jObj;
    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json.substring(json.indexOf("{"),
                    json.lastIndexOf("}") + 1));
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return jObj;

    }
}

I know that the provided URL is a valid JSON document, but my parser returns this:

{"MARGIN-LEFT":"30px","PADDING-BOTTOM":"2em","FONT-SIZE":"0.7em"} 

Why? What is happening?

EDIT:

The JSON is in norwegian, but the structure is easy to understand:

http://data.helsenorge.no/External.svc/Services/KA02/10.75/59.91

3
  • Looks like valid JSON to me. What's the URL? Commented Apr 12, 2013 at 20:05
  • Well, that is valid JSON. What were you expecting instead, content-wise? Also, you forgot to check your response status for success. Commented Apr 12, 2013 at 20:05
  • See the edit, there is the link. Commented Apr 12, 2013 at 20:07

2 Answers 2

1

I have compiled your code and run it, and I am not sure why the other Answer has been downvoted other than not giving much information on the subject. I changed your HttpPost to HttpGet and the JSON String I have had returned to me is indeed the JSON Contents of the site. Just change:

HttpPost httpPost = new HttpPost(url);

to

HttpGet httpGet = new HttpGet(url);

and Log or debug the JSON String that you get from

json = sb.toString();

and you should have exactly what you want. All you need to do then is parse the json string into a JSON Object and you are on your way.

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

1 Comment

I don't know why my answer was downvoted, I've written the same.. :)
0

I don't know this is the reason, but do you really want to do http post? If I see well, you just would like to call a service. Why don't use HttpGet?

Comments

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.