我試圖讓json數據進入textview,但是,它給出了一個錯誤。代碼如下。我想在textview中獲取JSON數據,但它給出了一個錯誤
這是我的配置類
package com.example.hp.eprincipal;
/**
* Created by hp on 4/15/2017.
*/
public class Config {
public static final String DATA_URL = "http://smcs.eprincipal.in/api/studentdetails.aspx?user_name=&password=";
public static final String KEY_REG = "Registration_No";
public static final String KEY_NAME = "Student_name";
public static final String KEY_ADDRESS = "address";
public static final String JSON_ARRAY = "result";
}
下面是代碼,我想有從URL數據去TextView的
ImageView std;
private EditText editTextId,editTextPass;
private Button buttonGet;
private TextView textViewResult;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_info);
std = (ImageView) findViewById(R.id.imageView2);
Picasso.with(getApplicationContext()).load("
http://www.shikshakiore.com/image/logo.png").into(std);
editTextId = (EditText) findViewById(R.id.editText6);
editTextPass = (EditText) findViewById(R.id.editText7);
buttonGet = (Button) findViewById(R.id.button2);
textViewResult = (TextView) findViewById(R.id.textView3);
buttonGet.setOnClickListener(this);
}
private void getData() {
String id = editTextId.getText().toString().trim();
String pass = editTextPass.getText().toString().trim();
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
if (pass.equals("")) {
Toast.makeText(this, "Please enter password", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+editTextId+editTextPass.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(My_info.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String Registration_No="";
String Student_name="";
String address = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
Registration_No = collegeData.getString(Config.KEY_REG);
Student_name = collegeData.getString(Config.KEY_NAME);
address = collegeData.getString(Config.KEY_ADDRESS);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("Registration No:\t"+Registration_No+"\nStudent Name:\t"+Student_name+"\nAddress:\t" +address);
}
@Override
public void onClick(View v) {
getData();
}
}
再次點擊大寫鎖定並輸入please ..好像被卡住了 –
*「但它給出錯誤」* **什麼**錯誤? ***嘆了口氣*** –
我建議刪除吶喊,然後你得到-8。 – halfer