0

On the PHP end, I'm getting a JSON Object that we're sending back to Android. The PHP code is straightforward and just echos the JSON object like I have seen in almost every tutorial.

This line creates an exception every other query. Am I doing something very wrong?

JSONObject json = jsonParser.makeHttpRequest(signInURL,
                    "POST", params1);

 04-15 21:40:44.787: E/AndroidRuntime(6325): java.lang.RuntimeException: An error occured while executing doInBackground()

I'm following these instructions:

How json will be used in our application.

  • When android application will execute, it will connect android device to PHP Script.
  • PHP Script will fetch data from the database. It will encode it into json format and send it to the device.
  • Now, android application will get these encoded data. It will parse the data and display it on android device.

In our Android application,

  • Use a HttpPost to get the data,
  • Convert response to string
  • Parse JSON data, and use it as you want

Edit: The problem was actually just PHP warnings that needed to be fixed. Thanks everyone!

1
  • Share you full error log. Commented Apr 16, 2014 at 2:06

3 Answers 3

3

set internet permission in AndroidManifest.xml :

<uses-permission android:name="android.permission.INTERNET"></uses-permission>   

Http request from android to server

public String httpRequest(String url, String query, int methodtype){

    try {
        String reqUrl = url + query;

        switch (methodtype) {
            case 1:
                HttpGet httpGet = new HttpGet(reqUrl);
                httpResponse = httpClient.execute(httpGet);
                break;
            case 2:
                HttpPost httpPost = new HttpPost(reqUrl);
                httpResponse = httpClient.execute(httpPost);
                break;

        }



        HttpEntity httpEntity = httpResponse.getEntity();
        instrObj = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        //HandleException
    } catch (ClientProtocolException e) {
        //HandleException
    } catch (IOException e) {
        //HandleException
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(instrObj, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        instrObj.close();
        json = sb.toString();

    } catch (Exception e) {

    }       
    return json;
}

For posting the form data to the server HttpPost is used and to retreive the data from server HttpGet is used....

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

Comments

1

It sometimes happens for not setting the internet permission.

To set internet permission do this:

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

Hope this helps.. :)

Comments

1

Try this piece of code

try {
    URL Url = new URL(" --------");
    HttpURLConnection connection = (HttpURLConnection) Url.openConnection();
    connection.connect();

    int responseCode = connection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK) {
        InputStream inputStream = connection.getInputStream();
        Reader reader = new InputStreamReader(inputStream);

        int contentLength = connection.getContentLength();
        char[] charArray = new char[contentLength];
        reader.read(charArray);

        JSONObject jsonResponse = new JSONObject(charArray.toString());
    }
    else {
        Log.i("", "HTTP Response Code: " + responseCode);
    }
}
catch (Exception e) {
}

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.