执行(onPostExecute)后,它将丢失ui格式

时间:2016-08-22 01:41:25

标签: android android-layout android-studio

我正在尝试在android中执行一个简单的http请求,然后使用页面的响应来实现我的textview。 textView是图像1的“示例”。 Image 1

问题是在方法调用之后,它会丢失在Image 2中暴露的字段的格式。

Image2

这是我的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>

这是我的活动代码:

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);
    }
}

2 个答案:

答案 0 :(得分:1)

android:layout_span

使用TextView
<TextView
        android:id="@+id/response"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_span="6"
        android:text="example"/>

答案 1 :(得分:1)

现在它没有采取全线,只有一半。但要解决消除其他领域的问题。 enter image description here