为什么我的json结果只在textview中显示一条记录?

时间:2015-02-03 07:51:07

标签: android

我是android的新手并且遇到了以下问题。这是我的异步任务,我想在下一个活动中显示结果。我在控制台中获得了整个结果,但在textview中,它只显示姓氏。

private class MyTask extends AsyncTask<String, String, String>{ 
        @Override


        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try{
                String link = "http://www.amtechnologies.org/emergency/get_medicine.php";           

                HttpClient client = new DefaultHttpClient();
                HttpPost request = new HttpPost();
                request.setURI(new URI(link));

                List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("disease_name", result2));
                System.out.println("Result2:"+result2);
                UrlEncodedFormEntity form = new UrlEncodedFormEntity(postParameters);
                request.setEntity(form);

                HttpResponse response = client.execute(request);
                HttpEntity entity = response.getEntity();

                String result=EntityUtils.toString(entity);
                result1 = result;

                System.out.println("Result: "+result);
                System.out.println("Result1: "+result1);

                Intent i= new Intent(Medicines.this,View_medicines.class);
                i.putExtra("result", result.toString());
                startActivity(i);

            }catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

    }   

下一次激活:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_medicines);

    mnm=(EditText) findViewById(R.id.m3);
    mi=(TextView) findViewById(R.id.m2);
    nextbtn=(Button) findViewById(R.id.okmedi);

    nextbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(View_medicines.this,Homepage.class);
            startActivity(myIntent);
            finish();
        }
    });



    try {
        Intent intent = getIntent();
        String jsonArray=intent.getStringExtra("result");
        jArray = new JSONArray(jsonArray);

        System.out.println("Universe Acitivy Json Array: "+jArray);

        for(int i=0;i<jArray.length();i++){
        String names=new JSONObject(jArray.getString(i)).get("medicine_name").toString();
        System.out.println(names+"");
        mnm.setText(names);
        mi.setText(""+new JSONObject(jArray.getString(i)).get("medicine_intake"));

        Toast.makeText(getApplicationContext(),""+new JSONObject(jArray.getString(i)).get("medicine_name").toString(), Toast.LENGTH_SHORT).show();

        }
        int SDK_INT = android.os.Build.VERSION.SDK_INT;
        if (SDK_INT > 8) 
        {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

更改此部分

for(int i=0;i<jArray.length();i++){
    String names=new JSONObject(jArray.getString(i)).get("medicine_name").toString();
    System.out.println(names+"");
    mnm.setText(names);
    mi.setText(""+new JSONObject(jArray.getString(i)).get("medicine_intake"));

    Toast.makeText(getApplicationContext(),""+new JSONObject(jArray.getString(i)).get("medicine_name").toString(), Toast.LENGTH_SHORT).show();

    }

以下

for(int i=0;i<jArray.length();i++){
    String names=new JSONObject(jArray.getString(i)).get("medicine_name").toString();
    System.out.println(names+"");
    mnm.append(names);
    mi.append(""+new JSONObject(jArray.getString(i)).get("medicine_intake"));

    Toast.makeText(getApplicationContext(),""+new JSONObject(jArray.getString(i)).get("medicine_name").toString(), Toast.LENGTH_SHORT).show();

    }