0

I've seen a lot this message error on this forum, but I still got my problem. Here's the thing: I have an Android app with a Form, I put text on the form and press send, once sent, that's in my data base. No problem of parsing when the database is in my local server (localhost)

BUT, I have the same database in a real server website, and when I'm trying to send the form JSON just send me "Error parsing data org.json.JSONException: End of input at character 0 of " I've checked, my php files are at the right place, connection variables are the right ones.

Here's my JSON parser :

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

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

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                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, "iso-8859-1"), 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);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}
10
  • What does the json string look like? Commented Mar 14, 2014 at 11:28
  • Thanks for the answer, where can I see that? Commented Mar 14, 2014 at 11:32
  • You can do Log.e("JSON Parser", "JSON String is " + json); before you try to convert it to a JSONObject. Commented Mar 14, 2014 at 11:45
  • Still the same error : but got"JSON String is ". and nothing other, so json is empty . Commented Mar 14, 2014 at 12:33
  • 'json' is probably empty because you're not getting it back from the url. How are you calling makeHttpRequest? Can you add an example in your question? Commented Mar 14, 2014 at 12:37

1 Answer 1

1

You can see your response as text byString response = EntityUtils.toString(httpEntity);

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

5 Comments

I put that on my JSON parser?
Could you paste here your JSON string?
I've put my JSONParser on the first post, where can I find JSON string??
ok i finally got this: 03-14 12:53:51.730: E/JSON Parser(16125): JSON String is, so it thinks that my string is empty...
i dont know why but now i've got this : java.lang.RuntimeException: An error occured while executing doInBackground(), never had before

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.