【发布时间】:2017-03-09 05:01:03
【问题描述】:
我想从 MySQL 中获取数据并在 textview 中显示。我在日志 cat 中出现错误,显示“org.json.JSONArray 类型的值 [] 无法转换为 JSONObject”。它在 Single Activity 中工作,而使用 Intent 它不获取数据。
主活动
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);
}
}
第二个活动
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";
【问题讨论】:
-
发布您的 json 响应 ...
-
我没有收到回复。
-
查看是否可以减少代码示例以隔离错误并准确显示您遇到的错误以及错误发生的位置。
-
您应该发布您的模型和 json 响应。那就是问题所在。当您的 json 是一个数组并且您想将其转换为对象模型时,您会收到此错误。
标签: android mysql android-volley