使用Volley从MySQL获取数据时出错

时间:2017-03-09 05:01:04

标签: android mysql android-volley

我想从MySQL获取数据并在textview中显示它。我在log cat中显示错误" org.json.JSONArray类型的值[]无法转换为JSONObject"。它适用于单一活动,而使用Intent则不提取数据。

MainActivity

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private EditText editTextId,ed1;
private Button buttonGet;




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

    editTextId = (EditText) findViewById(R.id.editTextId);
    buttonGet = (Button) findViewById(R.id.buttonGet);

    buttonGet.setOnClickListener(this);

}



@Override
public void onClick(View v) {

    Intent i =new Intent(MainActivity.this,SecondActivity.class);
    startActivity(i);
}
}

SecondActivity

public class SecondActivity extends AppCompatActivity {

private TextView textViewResult,tv,tv2,tv3,tv4,tv5,tv6;
private ProgressDialog loading;

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

    textViewResult = (TextView) findViewById(R.id.textViewResult);
    tv = (TextView) findViewById(R.id.textView);
    tv2 = (TextView) findViewById(R.id.textView2);
    tv3 = (TextView) findViewById(R.id.textView3);
    tv4 = (TextView) findViewById(R.id.textView4);
    tv5 = (TextView) findViewById(R.id.textView5);
    tv6 = (TextView) findViewById(R.id.textView6);
    getData();
}

private void getData() {
   // String id = editTextId.getText().toString().trim();

 //   if (id.equals("")) {
    //    Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
    //    return;
 //   }
    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

    String url = Config.DATA_URL;//+editTextId.getText().toString().trim();
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(SecondActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String response){
    String n="";
    String p2o5="";
    String k2o = "";
    String urea = "";
    String phos = "";
    String potash = "";
    try {
        /**/

       JSONObject jsonObject = new JSONObject(response);
        Log.e("response",""+jsonObject);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        for(int i=0;i<result.length();i++) {
            JSONObject collegeData = result.getJSONObject(i);
            n = collegeData.getString(Config.KEY_N);
            p2o5 = collegeData.getString(Config.KEY_P2o5);
            k2o = collegeData.getString(Config.KEY_k2o);
            urea = collegeData.getString(Config.KEY_urea);
            phos = collegeData.getString(Config.KEY_phos);
            potash = collegeData.getString(Config.KEY_potash);
        } } catch (JSONException e) {
        e.printStackTrace();
    }
    //   textViewResult.setText("Name:\t"+name+"\nEmail:\t" +address+ "\nAddress:\t"+ vc);
    //  textViewResult.setText("Name:\t"+name);
    textViewResult.setText("N:\t"+n);
    tv.setText("P2O5:\t"+p2o5);
    tv2.setText("K2O:\t"+k2o);
    tv4.setText("Urea:\t"+urea);
    tv5.setText("Phosphate:\t"+phos);
    tv6.setText("Potash :\t\t\t\t\t" +potash);
}
}

配置

public static final String DATA_URL = "http://........ag.php?id=";
public static final String KEY_N = "n";
public static final String KEY_P2o5 = "p2o5";
public static final String KEY_k2o = "k2o";
public static final String KEY_urea = "urea";
public static final String KEY_phos = "phos";
public static final String KEY_potash = "potash";
public static final String JSON_ARRAY = "agri_result_1";

1 个答案:

答案 0 :(得分:0)

谢谢大家,我得到了答案,它完美无缺。

主要活动

 private void getData() {

    String id = getIntent().getExtras().getString("id");
   ading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);


    String url = "http://...../ooo.php?id=";

    StringRequest stringRequest = new StringRequest(url, new       Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();
            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(SecondActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

第二项活动

{{1}}