I am trying to do one simple http request in android and then atualize my textview with the response of the page. The textView is the "example" at image 1.
The problem is that after the method call, it looses the formate of the fields as exposed at the Image 2.
This is my xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<EditText android:id="@+id/edit_nrdoc"
android:layout_width="wrap_content"
android:inputType="number"
android:maxLength="6"
android:hint="@string/edit_nrdoc" />
<TextView
android:layout_width="5dp"
android:text="."
android:layout_height="wrap_content"
android:textStyle="bold"/>
<EditText android:id="@+id/edit_dv"
android:inputType="number"
android:maxLength="1"
android:hint="@string/edit_dv" />
<TextView
android:layout_width="5dp"
android:text="/"
android:textStyle="bold"/>
<EditText android:id="@+id/edit_andoc"
android:inputType="number"
android:maxLength="4"
android:hint="@string/edit_andoc" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="sendMessage"
android:text="@string/button_send" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="example"/>
</TableRow>
And this is my Activity code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* Called when the user clicks the Send button
*/
public void sendMessage(View view) throws IOException {
EditText nrdoc = (EditText) findViewById(R.id.edit_nrdoc);
EditText dv = (EditText) findViewById(R.id.edit_dv);
EditText andoc = (EditText) findViewById(R.id.edit_andoc);
String str = "http://www.google.com";
new DownloadWebpageTask().execute(str);
}
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
TextView response = (TextView) findViewById(R.id.response);
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
response.setText(result);
}
}
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
}