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?