1

public class MainActivity extends AppCompatActivity {

EditText editmsg , editmail ;
TextView content ;
String Message , Email ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editmsg = (EditText) findViewById(R.id.editMessage);
    editmail = (EditText) findViewById(R.id.editMail);
    Button btnsubmit = (Button) findViewById(R.id.btnSubmit);
    btnsubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                GetText();
            } catch (Exception ex) {
                content.setText(" Url Exception!");
            }
        }
    });
}

public void GetText()throws UnsupportedEncodingException {

    Message = editmsg.getText().toString();
    Email = editmail.getText().toString();

    String data = URLEncoder.encode("message", "UTF-8")
            + "=" + URLEncoder.encode(Message, "UTF-8");

    data += "&" + URLEncoder.encode("email", "UTF-8") + "="
            + URLEncoder.encode(Email, "UTF-8");

    String text = "";
    BufferedReader reader=null;
    HttpURLConnection urlConnection = null;

    try
    {

        // Defined URL  where to send data
        URL url = new URL("http://nitesh.com/feedback/add.php");

        // Send POST data request


        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
        wr.write( data );
        wr.flush();

        // Get the server response

        reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while((line = reader.readLine()) != null)
        {
            // Append server response in string
            sb.append(line + "\n");
        }


        text = sb.toString();
    }
    catch(Exception ex)
    {

    }
    finally
    {
        try
        {

            reader.close();
        }

        catch(Exception ex) {}
    }

    // Show response on activity
    content.setText( text  );

}

}

I dont know what is wrong with code when i click the send button it showing Unfortunately,HTTP has stop (looking just like app crashed). please review my code and tell me what i have done wrong.

3 Answers 3

1

One thing I see at once is that you are making your request in main UI thread (in onCreate). This is not allowed because network connections usually take some time to finish. There should also be an error message in the logcat about this.

What you should do is make your request in a separate thread. You can accomplish this by using AsyncTask, or Thread. Google it.

UPD:

Example on how to use Threads in Java. The method run will be executed asynchronously.

new Thread(new Runnable(){
  @Override
  public void run(){
    GetText();
  }
}).start();

Next time, please include the logcat errors in the question.

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

4 Comments

FATAL EXCEPTION: main java.lang.NullPointerException
@Nitesh You get this because in your catch block, content is null. Define it in onCreate.
if you dont mind can you please correct my code . So that i can understand the whole situation
@NiteshKhaitan Sorry for a late reply. See an edit please.
0

Consider using http://square.github.io/retrofit/ for network communication.

You will get a simple api and all request will be wrapped into correct thread.

2 Comments

If you are going to program for Android, you should get used to creating and doing things in different threads. Using a specialised library for that is an overkill
This is not a lib for threads but a type-safe HTTP client for Android and Java
0

You can accomplish this task by using AsyncTask

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.