Hi Dw.

This is my first Android app, and what I'm trying to do is to post a POST/GET method to my webserver using Http in Android studio.

I also have a Textview which suppose to display echoed data from server which will be a response to a request since the php file I'm posting to simply echo back the data.

Here is what I have:

try {
            URL url = new URL("http://myURLAddress/run.php");
            JSONObject postDataParams = new JSONObject();
            postDataParams.put("name", "Mr.M");
            Log.e("params", postDataParams.toString());

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));


            int responseCode=conn.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK){
                BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                while ((line = in.readLine()) != null){
                //return sb.toString();"Message Sent."
                //return new String("false : " +responseCode);
        }catch (MalformedURLException e){
        }catch (IOException e){
        }catch (JSONException e){
        } catch (NetworkOnMainThreadException e){
        } catch (Exception e) {

I get a 200 respond and with this line MainActivity.tvresult.setText(sb.toString()); on Textview I see some HTML code not data that was echoed by server.

How can I get the data server echoed?

Recommended Answers

All 3 Replies

More info needed:
Exactly what response did you expect, and exactly what did you get? "some HTML code" is no help.

The html was the requirement of cookies, I've found a solution to it, but I dont get anything back now.

Basically I send a key with a value to my php file and the only thing that php file does is echo the POST/GET data which is what I should see on my Textview which is what I'm trying to achieve in line 33, which means I should see what I sent to the server back.

Also I just don't think my code does send the post/get method because on the server I have a php file that should write a file with what ever was posted to it and then return back the post data by means of echo but if I post via my app nothing happens on the server but if I use some other tools like postman the file is created and I get the data back.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.