我收到错误org.json.JSONObject无法转换为JSON数组。我试图在这里实施一些解决方案,但没有成功。我正在使用的教程实现了一个响应,当我编辑时,有breakS。我想确保我正确地做到了。我感谢任何帮助。
来自我的activity.java
private void getJSON() {
swipeRefreshLayout.setRefreshing(true);
String url = URL_Alerts;
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
if (response.length() > 0) {
// looping through json and adding to movies list
for (int i = 0; i < response.length(); i++) {
try {
JSONObject alertObj = response.getJSONObject(i);
int id = alertObj.getInt("id");
String image = alertObj.isNull("image") ? null:alertObj
.getString("image");
String name = alertObj.getString("name");
String date = alertObj.getString("date");
String order = alertObj.getString("order");
String pair = alertObj.getString("pair");
String rate = alertObj.getString("rate");
String quick_target = alertObj.getString("quick_target");
String long_target = alertObj.getString("long_target");
String stop_loss = alertObj.getString("stop_loss");
String break_even = alertObj.getString("break_even");
String status = alertObj.getString("status");
String profileImageView = alertObj.getString("profilePic");
String feedImageView = alertObj.getString("feedImageView");
Alert a = new Alert(id, name, date, order, pair, rate,
quick_target, long_target,stop_loss, break_even,
status, profileImageView, feedImageView, image);
alertList.add(0, a);
// updating offset value to highest value
if (id >= offSet)
offSet = id;
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
});
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(req);
}
这是php文件
<?php
//Importing Database Script
require_once('dbConnectalerts.php');
//Creating sql query
$sql = "SELECT * FROM alerts";
//getting result
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"id"=>$row['id'],
"order"=>$row['order'],
"pair"=>$row['pair'],
"rate"=>$row['rate'],
"quick_target"=>$row['quick_target'],
"long_target"=>$row['long_target'],
"stop_loss"=>$row['stop_loss'],
"break_even"=>$row['break_even'],
"name"=>$row['name'],
"status"=>$row['status']
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
这是从网址中提取时的输出。
{"result":[{"id":"1","order":"BUY","pair":"EURGBP","rate":"1.6025","quick_target":"1.6035","long_target":"1.6055","stop_loss":"1.5975","break_even":"1.6030","name":"EURGBP","status":"BETA ENTRY"},{"id":"2","order":"Sell","pair":"GBPAUD","rate":"1.5050","quick_target":"1.5040","long_target":"1.5010","stop_loss":"1.5099","break_even":"1.5040","name":"GBPAUD","status":"Beta testing"}]}
以下是我使用提供的解决方案进行编辑时所获得的内容。
private void getJSON() {
swipeRefreshLayout.setRefreshing(true);
String url = URL_Alerts;
JSONObject object = new JSONObject(result); //cannot resolve symbol result
JSONArray Jarray = object.getJSONArray("result"); //unhandled exception
for (int i = 0; i < Jarray.length(); i++) { //variable i is already handled in the scope
JSONObject Jasonobject = Jarray.getJSONObject(i); //unhandled exception
for (int i = 0; i < Jarry.length(); i++) { //cannot resolve symbol Jarry
try {
JSONObject alertObj = Jarry.getJSONObject(i);
int id = alertObj.getInt("id");
String image = alertObj.isNull("image") ? null:alertObj
.getString("image");
String name = alertObj.getString("name");
String date = alertObj.getString("date");
String order = alertObj.getString("order");
String pair = alertObj.getString("pair");
String rate = alertObj.getString("rate");
String quick_target = alertObj.getString("quick_target");
String long_target = alertObj.getString("long_target");
String stop_loss = alertObj.getString("stop_loss");
String break_even = alertObj.getString("break_even");
String status = alertObj.getString("status");
String profileImageView = alertObj.getString("profilePic");
String feedImageView = alertObj.getString("feedImageView");
Alert a = new Alert(id, name, date, order, pair, rate,
quick_target, long_target,stop_loss, break_even,
status, profileImageView, feedImageView, image);
alertList.add(0, a);
// updating offset value to highest value
if (id >= offSet)
offSet = id;
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
});
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(req);
}
}
这是完整的java类。
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import com.brasiltradefx.btfxalertsadmin.app.MyApplication;
import com.brasiltradefx.btfxalertsadmin.helper.Alert;
import com.brasiltradefx.btfxalertsadmin.helper.SwipeListAdapter;
/**
* Created by Deryk on 12/2/2015.
*/
public class AlertMainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
private static final String TAG = AlertMainActivity.class.getSimpleName();
private String URL_Alerts = "http://www.contrariantradefx.info/android/btfxalerts/alerts_all_data.php";
private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private SwipeListAdapter adapter;
private List<Alert> alertList;
private int offSet = 0;
private String JSON_STRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_main);
listView = (ListView) findViewById(R.id.listView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
alertList = new ArrayList<>();
adapter = new SwipeListAdapter(this, alertList);
listView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(this);
/**
* Showing Swipe Refresh animation on activity create
* As animation won't start on onCreate, post runnable is used
*/
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
getJSON();
}
}
);
}
@Override
public void onRefresh() {
getJSON();
}
@Override
protected void onResume() {
super.onResume();
getJSON();
}
private void getJSON() {
swipeRefreshLayout.setRefreshing(true);
String url = URL_Alerts;
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
if (response.length() > 0) {
// looping through json and adding to movies list
for (int i = 0; i < response.length(); i++) {
try {
JSONObject alertObj = response.getJSONObject(i);
int id = alertObj.getInt("id");
String image = alertObj.isNull("image") ? null:alertObj
.getString("image");
String name = alertObj.getString("name");
String date = alertObj.getString("date");
String order = alertObj.getString("order");
String pair = alertObj.getString("pair");
String rate = alertObj.getString("rate");
String quick_target = alertObj.getString("quick_target");
String long_target = alertObj.getString("long_target");
String stop_loss = alertObj.getString("stop_loss");
String break_even = alertObj.getString("break_even");
String status = alertObj.getString("status");
String profileImageView = alertObj.getString("profilePic");
String feedImageView = alertObj.getString("feedImageView");
Alert a = new Alert(id, name, date, order, pair, rate,
quick_target, long_target,stop_loss, break_even,
status, profileImageView, feedImageView, image);
alertList.add(0, a);
// updating offset value to highest value
if (id >= offSet)
offSet = id;
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
});
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(req);
}
}
万岁!问题解决了。我做了一些调整,现在错误消失了。感谢您的帮助Prerak Sola!
最终修改
enter code h private void getJSON() {
swipeRefreshLayout.setRefreshing(true);
String url = URL_Alerts;
JsonObjectRequest req = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
//Log.d(TAG, response.toString());
JSONArray jsonArray = null;
try {
jsonArray = jsonObject.getJSONArray("result");
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonArray.length() > 0) {
// looping through json and adding to movies list
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject alertObj = jsonArray.getJSONObject(i);
int id = alertObj.getInt("id");
String image = alertObj.isNull("image") ? null : alertObj
.getString("image");
String name = alertObj.getString("name");
String date = alertObj.getString("date");
String order = alertObj.getString("order");
String pair = alertObj.getString("pair");
String rate = alertObj.getString("rate");
String quick_target = alertObj.getString("quick_target");
String long_target = alertObj.getString("long_target");
String stop_loss = alertObj.getString("stop_loss");
String break_even = alertObj.getString("break_even");
String status = alertObj.getString("status");
String profileImageView = alertObj.getString("profilePic");
String feedImageView = alertObj.getString("feedImageView");
Alert a = new Alert(id, name, date, order, pair, rate,
quick_target, long_target, stop_loss, break_even,
status, profileImageView, feedImageView, image);
alertList.add(0, a);
// updating offset value to highest value
if (id >= offSet)
offSet = id;
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
});
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(req);
}
答案 0 :(得分:0)
private void getJSON() {
swipeRefreshLayout.setRefreshing(true);
String url = URL_Alerts;
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject jsonObject) {
Log.d(TAG, response.toString());
JSONArray jsonArray = jsonObject.getJSONArray("result");
if (jsonArray.length() > 0) {
// looping through json and adding to movies list
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject alertObj = jsonArray.getJSONObject(i);
int id = alertObj.getInt("id");
String image = alertObj.isNull("image") ? null:alertObj
.getString("image");
String name = alertObj.getString("name");
String date = alertObj.getString("date");
String order = alertObj.getString("order");
String pair = alertObj.getString("pair");
String rate = alertObj.getString("rate");
String quick_target = alertObj.getString("quick_target");
String long_target = alertObj.getString("long_target");
String stop_loss = alertObj.getString("stop_loss");
String break_even = alertObj.getString("break_even");
String status = alertObj.getString("status");
String profileImageView = alertObj.getString("profilePic");
String feedImageView = alertObj.getString("feedImageView");
Alert a = new Alert(id, name, date, order, pair, rate,
quick_target, long_target,stop_loss, break_even,
status, profileImageView, feedImageView, image);
alertList.add(0, a);
// updating offset value to highest value
if (id >= offSet)
offSet = id;
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
});
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(req);
}