Connection android webservice

Asked

Viewed 256 times

0

Hello, folks I am developing an app in the Android Studio IDE and am having problems connecting to my webservice. I am receiving error: D/Answer: {"Success":"false","message":"A Jsonobject text must Begin with '{' at Character 1 of Location=25"}, follows the code. Does anyone know how I can solve this problem? Thank you very much.

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://MY_HOST");
        httpPost.setHeader("Content-Type","application/json;charset=UTF-8");
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("location","25"));

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            serverData = EntityUtils.toString(httpEntity);
            Log.d("response", serverData);

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

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

        }

1 answer

0

Your PHP file is not generating JSON correctly, this may be the cause. Test in the browser and see if it is generating JSON, if it is Jsonarray, starting with [, or if it is Jsonobject, starting with {. The log you are capturing is from the message returned in the connection, the same as written in the web file using echo, if it is in PHP.

  • I only replied at first because I couldn’t add comments ><

  • I am receiving this message in Logcat. The problem is that when I convert to Json Object before sending it does not set as parameter to '{'.

  • Add your JSON received from Webservice to the question for ease of understanding.

  • See if this can help: stackoverflow.com/questions/19399135/a-jsonobject-text-must-Begin-with-error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.