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 */);
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);
            conn.setDoInput(true);

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

            writer.flush();
            writer.close();
            os.close();

            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){
                    sb.append(line);
                    break;
                }
                in.close();
                //return sb.toString();"Message Sent."
                MainActivity.tvresult.setText(sb.toString());
            }else{
                //return new String("false : " +responseCode);
                MainActivity.tvresult.setText(responseCode);
                //MainActivity.tvresult.append(,responseCode);
            }
        }catch (MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }catch (JSONException e){
            e.printStackTrace();
        } catch (NetworkOnMainThreadException e){
            //MainActivity.tvresult.setText(e);
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

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?

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.