2

I'm trying to upload a name and a score to with a HTTP post request on a button click, but I get a weird error. Here is my code.

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
        case R.id.bMenu:
            Intent i = new Intent(ShowScore.this, Menu.class);
            startActivity(i);
            break;
        case R.id.bUpload:
            postData();
            break;
    }
}

private void postData() {

    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.ratemyplays.com/form.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("name", "Steve"));
        nameValuePairs.add(new BasicNameValuePair("score", "24"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        // HttpResponse response = httpclient.execute(httppost);

        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);

        // Just display the response back
        displayToastMessage(responseBody);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

private void displayToastMessage(String responseBody) {
    // TODO Auto-generated method stub
}

Here is my error

Invalid layout of java.lang.String at value

A fatal error has been detected by the Java Runtime Environment:

Internal Error (javaClasses.cpp:136), pid=9736, tid=10668 fatal error: Invalid layout of preloaded class

JRE version: (7.0_40-b43) (build ) Java VM: Java HotSpot(TM) 64-Bit Server VM (24.0-b56 mixed mode windows-amd64 compressed oops) Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

6
  • Please print the entire stack trace. Commented Dec 30, 2013 at 14:41
  • 1
    Is this part of an Android project and are you trying to execute as a Java application? Commented Dec 30, 2013 at 14:48
  • 1
    Android tag is missing in the question Commented Dec 30, 2013 at 14:49
  • Is this actually an android question? Commented Dec 30, 2013 at 14:56
  • If it's NetworkOnMainThreadException, create and AsyncTask and call the postData() method from inside the AsyncTask's doInBackground() method. To get started, here are the basics: developer.android.com/reference/android/os/AsyncTask.html If you have ever worked with C#/.NET's BackgroundWorker, it's essentially the same idea... Commented Dec 30, 2013 at 15:14

2 Answers 2

2

If you look through the stack trace you'll probably find a NetworkOnMainThreadException. You cannot run network operations on the same thread as the UI since Android 3.x and above.

in summary, you'll need to use an ASyncTask

Source: Android IllegalStateException: Could not execute method of the activity (I tested the code myself and followed the stack trace to discover this answer)

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

2 Comments

A simple execution of a main method with absolute no other operations in MainActivity implementation class would yield the same error, without any network ops per se. It has to be executed as an Android Application and not as a Java one within ADT
Thank you, i found out that using a new thread did the trick, but your answer lead me on the right track.
0

It's Eclipse that crashes. It's not really related to your Android code.

Have a look at Fatal Error: Invalid Layout of java.lang.String at value for fixing it.

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.