Arrays JSONArray没有值
JSONArray的问题。这是我的密码,帮帮我 这是我的JsonObject和错误。。 Json错误:{“用户名”:“rafyluc”,“记录”:“500”}{“用户名”:“inkinati”,“记录”:“600”}{“用户名”:“rafyluc”,“记录”:“500”}{“用户名”:“inkinati”,“记录”:“600”}Arrays JSONArray没有值,arrays,Arrays,JSONArray的问题。这是我的密码,帮帮我 这是我的JsonObject和错误。。 Json错误:{“用户名”:“rafyluc”,“记录”:“500”}{“用户名”:“inkinati”,“记录”:“600”}{“用户名”:“rafyluc”,“记录”:“500”}{“用户名”:“inkinati”,“记录”:“600”} 公共类ListaAlanni扩展了AppCompative活动{ 私有静态ArrayList alunni; AlunniListAdapter自定义适配器; 私有字符
公共类ListaAlanni扩展了AppCompative活动{
私有静态ArrayList alunni;
AlunniListAdapter自定义适配器;
私有字符串标记=listaaluni.class.getSimpleName();
专用静态字符串url=”http://192.168.1.11:80/webservice/lista.php";
ArrayList项目列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_alunni);
getSupportActionBar().setTitle(“Lista Alunni”);
alunni=新阵列列表(10);
popola();
}
私有void setTextLista(){
ListView ll=(ListView)findViewById(R.id.lista);
ll.setAdapter(新的AlunniListAdapter(Listaaluni.this,R.layout.lista_row,alunni));
}
私人空房{
新建异步任务(){
@凌驾
受保护的void onPreExecute(){
alunni=新阵列列表(10);
}
@凌驾
受保护对象doInBackground(对象…参数){
HttpHandler sh=新的HttpHandler();
字符串jsonStr=sh.makeServiceCall(url);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
Log.d(“Debug_对象”,String.valueOf(jsonObj));
//获取JSON数组节点
//试一试{
//JSONArray jArray=新JSONArray(jsonStr);
JSONArray jArray=jsonObj.getJSONArray(jsonStr);
Log.d(“DEBUG_json”,String.valueOf(jArray));
for(int i=0;i
看看您的代码,似乎是JSONArray jArray=jsonObj.getJSONArray(jsonStr);
是导致问题的原因
jsonObj.getJSONArray
要求传递属性的名称,例如jsonObj.getJSONArray(“数组_属性”)
但是,您似乎是
public class ListaAlunni extends AppCompatActivity {
private static ArrayList<Alunni> alunni;
AlunniListAdapter customAdapter;
private String TAG = ListaAlunni.class.getSimpleName();
private static String url = "http://192.168.1.11:80/webservice/lista.php";
ArrayList<HashMap<String, String>> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_alunni);
getSupportActionBar().setTitle("Lista Alunni");
alunni = new ArrayList<Alunni>(10);
popola();
}
private void setTextLista() {
ListView ll = (ListView) findViewById(R.id.lista);
ll.setAdapter(new AlunniListAdapter(ListaAlunni.this, R.layout.lista_row, alunni));
}
private void popola() {
new AsyncTask<Object, Object, Object>() {
@Override
protected void onPreExecute() {
alunni = new ArrayList<Alunni>(10);
}
@Override
protected Object doInBackground(Object... params) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
Log.d("Debug_object", String.valueOf(jsonObj));
// Getting JSON Array node
// try {
//JSONArray jArray=new JSONArray(jsonStr);
JSONArray jArray = jsonObj.getJSONArray(jsonStr);
Log.d("DEBUG_json", String.valueOf(jArray));
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Log.i("TEST", "Username: " + json_data.getString("Username") +
", record: " + json_data.getString("Record")
);
String nome= json_data.getString("Username");
String record=json_data.getString("Record");
// alunni.add(new Alunni(nome,record));
HashMap<String, String> item = new HashMap<>();
item.put("Username", nome);
item.put("Record", record);
// adding item to item list
itemList.add(item);
}
} catch (final JSONException e) {
Log.e(TAG, "Json errore: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json errore: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Object o) {
setTextLista();
}
}.execute();
}
}
classe HttpHandler:
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}