【问题标题】:How to set a text from my AsyncTask to a textview in a fragment?如何将我的 AsyncTask 中的文本设置为片段中的文本视图?
【发布时间】:2014-08-03 06:39:32
【问题描述】:

这里是我的 OnCreateView 代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_profile, container, false);
    tvMemberName = (TextView) v.findViewById(R.id.member_name);
    UrlPostHelper uph = new UrlPostHelper();
    uph.execute();
    return v;
}

和异步任务

    private class UrlPostHelper extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... params) {
        String url = "http://localhost:8080/MP/Profile";
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse response;
        String data = "no response";
        try {
            response = httpClient.execute(httpGet);
            data = EntityUtils.toString(response.getEntity());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String s = "yow";


        try {
            JSONArray ja = new JSONArray(data);
            for (int i = 0 ; i < ja.length(); i++ ){
                JSONObject j = ja.getJSONObject(i);
                String firstName = j.getString("firstName");
                String lastName = j.getString("lastName");
                System.out.println(firstName);
                s = firstName +" " + lastName;
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return s;

    }

    protected void onPostExecute(String result){
        super.onPostExecute(result);
        Log.i("TAG", result );
        tvMemberName.setText(result);
    }


}

我的问题是,在我的 android 应用程序中,它一开始没有显示任何内容,但大约 1-2 分钟后会显示“yow”。但是,它不会被成员名称替换。我尝试运行我的 servlet,它工作得很好。请帮我。

【问题讨论】:

  • onPostExecute 中'result'的值是多少?
  • servlet 在data 中得到什么响应?
  • 我猜,你只是得到了 JSONException。检查 LogCat 的堆栈跟踪。

标签: java android android-fragments android-asynctask textview


【解决方案1】:
  • AsyncTask&lt;Params, Progress, Result&gt;.

    private class UrlPostHelper extends AsyncTask<Void, Void, String>{
    private EditText tvMemberName;
    public UrlPostHelper(EditText tv){
      this.tvMemberName = tv;
    }
    
  • 后台任务完成后返回文本。

    @Override
    protected String doInBackground(Void... params) {
    
    HttpResponse response;
    String data = "no response";
    try {
        response = httpClient.execute(httpGet);
        data = getJSONString(response.getEntity());
    } catch (Exception e) {
        e.printStackTrace();
    }
    
       return data; //Text to set TvMember Value
    }
    
  • 从响应 Util 中获取字符串。

    public String getJSONString(InputStream is) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    String inputLine;
    StringBuffer response = new StringBuffer();
    
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    return response.toString();
    } 
    
  • 在PostExecute 上设置EditText。

    protected void onPostExecute(String result){
       Log.i("TAG", result );
       tvMemberName.setText(result);
    }
    
  • 使用 UrlPostHelper 调用传递 EditText。

    new UrlPostHelper(tvMemberName).excute();
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    相关资源
    最近更新 更多