无法在postExecute,Async,Android中设置文本

时间:2016-08-26 04:03:02

标签: java android json android-asynctask

我无法为以下问题找到任何合适的解决方案。我正在使用AsyncTask应用程序发送请求到服务器并返回JSON数组作为响应,在postExecute方法中我解析它,问题是当我尝试将解析数据设置为TextView时,textview不显示数据。我确信服务器返回了一些数据,这些数据在postExecute中解析并保存在全局变量中。 TextViews也被声明为全局变量,并在OnCreate方法中定义。提前致谢! 请检查下面提到的代码:

public class CompanyData extends AppCompatActivity implements View.OnClickListener {

    Button cComments;

    String ssid,bin;
    String extra, extra1;
    TextView compData1, compData2, compData3, compData4, compData5, compData6, compTitle;
    String title,  kod_okpo, address, reg_date, fio, kod_oked, ovd  ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_data);

        cComments = (Button) findViewById(R.id.cComment);
        cComments.setOnClickListener(this);

        Bundle extras = getIntent().getExtras();

        if (extras != null) {
            extra = extras.getString("bin");
            extra1 = extras.getString("ssid");
            send_company_req(extra1, extra);
        }

        compTitle = (TextView) findViewById(R.id.companyTitle);
        compData1 = (TextView) findViewById(R.id.compData1);
        compData2 = (TextView) findViewById(R.id.compData2);
        compData3 = (TextView) findViewById(R.id.compData3);
        compData4 = (TextView) findViewById(R.id.compData4);
        compData5 = (TextView) findViewById(R.id.compData5);
        compData6 = (TextView) findViewById(R.id.compData6);
        //Toast.makeText(this,"LOOOL" + title+bin+kod_okpo+address+reg_date+fio+kod_oked+ovd, Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cComment:
                Intent companyData = new Intent(CompanyData.this, Comments.class);
                companyData.putExtra("bin", bin);
                companyData.putExtra("ssid", ssid);
                startActivity(companyData);

                startActivity(new Intent(this, Comments.class));
                break;
        }
    }

    private void send_company_req(final String ssid, final String searchData) {
        class GetJSON extends AsyncTask<String, String, String> {
            ProgressDialog loading;
            String rStr;



            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(CompanyData.this, "Request...", null, true, true);
            }

            @Override
            protected String doInBackground(String... params) {
                String token = params[0];
                String fi = params[1];
                String uri = Quickstart.URL + "/car/info";


                String param = null;

                try {
                    param = "ssid=" + URLEncoder.encode(token, "UTF-8") +
                            "&bin=" + URLEncoder.encode(fi, "UTF-8") + "&dev=android";
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                try {
                    URL url = new URL(uri);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setDoOutput(true);
                    con.setRequestMethod("POST");
                    con.setFixedLengthStreamingMode(param.getBytes().length);
                    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    con.setRequestProperty("Authorization", "Bearer " + token);
                    PrintWriter out = new PrintWriter(con.getOutputStream());
                    out.print(param);
                    out.close();
                    String response = "";
                    Scanner inStream = new Scanner(con.getInputStream());
                    while (inStream.hasNextLine()) {
                        response += (inStream.nextLine());
                    }

                    return response;

                } catch (Exception e) {
                    return null;
                }
            }
            @Override
            protected void onPostExecute(String s) {

                super.onPostExecute(s);
                loading.dismiss();
                //Toast.makeText(CompanyData.this, s, Toast.LENGTH_LONG).show();
                JSONArray jsonArrayComp;



                try {

                    jsonArrayComp = new JSONArray(s.trim());


                    JSONObject jsonObjectComp = jsonArrayComp.getJSONObject(0);
                    try {



                        title = jsonObjectComp.getString("title");
                        kod_okpo = jsonObjectComp.getString("kod_okpo");
                        address = jsonObjectComp.getString("address");
                        reg_date = jsonObjectComp.getString("reg_date");
                        fio = jsonObjectComp.getString("fio");
                        kod_oked = jsonObjectComp.getString("kod_1_oked");
                        ovd = jsonObjectComp.getString("vidd");
                        Toast.makeText(CompanyData.this,"LOOOL" + title+bin+kod_okpo+address+reg_date+fio+kod_oked+ovd, Toast.LENGTH_LONG).show();



                    } catch (Exception ee) {

                    }


                } catch (Exception e) {
                    //Toast.makeText(CompanyData.this, "Упс,:( что то пошло не так, попробуйте еще раз пожалуйста.", Toast.LENGTH_SHORT).show();

                }


                compTitle.setText(title);
                compData1.setText(bin);
                compData2.setText(kod_okpo);
                compData3.setText(address);
                compData4.setText(reg_date);
                compData5.setText(fio);
                compData6.setText(kod_oked + " - " + ovd);

            }

        }

        GetJSON gj = new GetJSON();
        gj.execute(ssid, searchData);
    }
}

0 个答案:

没有答案