我试图从网站http://www.spmsejarahscore.epizy.com/androidbackend/spmpast.php中检索jsonArray。 Volley获得JsonArray可以在locahost上显示列表视图,但是当我在网站上实现spmpast.php时,列表视图输出在实际设备上为空并在模拟器上崩溃。我希望在这里寻求指导,因为这是我的最终项目。
MainActivity.xml
public class SpmPastActivity extends CodeReuse {
// 185.27.134.131
// www.spmsejarahscore.epizy.com
public static final String JSON_URL = "http://www.spmsejarahscore.epizy.com/androidbackend/spmpast.php";
public ListView listView;
private ProgressBar loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spm_past);
barRule();
getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_arrow_left_white);
sendRequest();
}
private void sendRequest(){
loading = (ProgressBar) findViewById(R.id.progressBar);
StringRequest stringRequest = new StringRequest(Request.Method.GET,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.wtf("RESPONSE", response);
showJSON(response);
loading.setVisibility(View.INVISIBLE);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SpmPastActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.sParseJSON();
SCustomList cl = new SCustomList(this, ParseJSON.ids, ParseJSON.years, ParseJSON.pdfs);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(cl);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ViewGroup vg = (ViewGroup) view;
TextView tv = (TextView) vg.findViewById(R.id.stateYear);
String url = tv.getTag().toString();
// Toast.makeText(SpmPastActivity.this, url, Toast.LENGTH_LONG).show();
String doc = "https://docs.google.com/viewer?url=" + url;
Intent intent = new Intent(SpmPastActivity.this, PdfFullscreenActivity.class);
intent.putExtra("PDF_TAG", doc);
startActivity(intent);
}
});
}
ParseJson.java:
public class ParseJSON {
public static String[] ids;
public static String[] years;
public static String[] pdfs;
public static final String JSON_ARRAY = "result";
public static final String KEY_ID = "id";
public static final String KEY_YEAR = "year";
public static final String KEY_PDF = "pdf_path";
private JSONArray users = null;
private String json;
public ParseJSON(String json){
this.json = json;
}
protected void sParseJSON(){
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
ids = new String[users.length()];
years = new String[users.length()];
pdfs = new String[users.length()];
for(int i=0;i < users.length();i++){
JSONObject jo = users.getJSONObject(i);
ids[i] = jo.getString(KEY_ID); // work on loaclhost
years[i] = "SPM Year " + jo.getString(KEY_YEAR); // work on loaclhost
pdfs[i] = "http://www.spmsejarahscore.epizy.com/web/media/pdf/" + jo.getString(KEY_PDF);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
logcat的:
07-27 06:37:13.465 1591-1603/system_process I/ActivityManager: START u0 {act=com.foong.spmsejarahscore.SpmPastActivity cmp=com.foong.spmsejerahscore/.SpmPastActivity} from pid 12003
07-27 06:37:13.605 12003-12003/com.foong.spmsejerahscore D/EXECUETE: before ProgressBar
07-27 06:37:13.615 12003-12003/com.foong.spmsejerahscore D/EXECUETE: before StringRequest
07-27 06:37:13.635 12003-12118/com.foong.spmsejerahscore D/dalvikvm: GC_FOR_ALLOC freed 322K, 2% free 24342K/24732K, paused 10ms, total 13ms
07-27 06:37:13.825 12003-12003/com.foong.spmsejerahscore D/EGL_emulation: eglMakeCurrent: 0xb8b613a0: ver 2 0
07-27 06:37:14.475 12003-12003/com.foong.spmsejerahscore I/Choreographer: Skipped 36 frames! The application may be doing too much work on its main thread.
07-27 06:37:14.585 12003-12003/com.foong.spmsejerahscore D/EGL_emulation: eglMakeCurrent: 0xb8b613a0: ver 2 0
07-27 06:37:14.625 1591-1605/system_process I/ActivityManager: Displayed com.foong.spmsejerahscore/.SpmPastActivity: +1s117ms
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore D/RESPONSE: <html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("72b0c45f6fbda01cced41959e9e196a1");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://www.spmsejarahscore.epizy.com/androidbackend/spmpast.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at org.json.JSONObject.<init>(JSONObject.java:159)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at org.json.JSONObject.<init>(JSONObject.java:172)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.foong.spmsejerahscore.ParseJSON.sParseJSON(ParseJSON.java:88)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.foong.spmsejerahscore.SpmPastActivity.showJSON(SpmPastActivity.java:76)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.foong.spmsejerahscore.SpmPastActivity.access$000(SpmPastActivity.java:22)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.foong.spmsejerahscore.SpmPastActivity$1.onResponse(SpmPastActivity.java:59)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.foong.spmsejerahscore.SpmPastActivity$1.onResponse(SpmPastActivity.java:55)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at android.os.Looper.loop(Looper.java:136)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore D/AndroidRuntime: Shutting down VM
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4cc8b20)
07-27 06:37:14.725 12003-12003/com.foong.spmsejerahscore E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.foong.spmsejerahscore, PID: 12003
java.lang.NullPointerException: storage == null
at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
at java.util.Arrays.asList(Arrays.java:155)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at com.foong.spmsejerahscore.SCustomList.<init>(SCustomList.java:34)
at com.foong.spmsejerahscore.SpmPastActivity.showJSON(SpmPastActivity.java:77)
at com.foong.spmsejerahscore.SpmPastActivity.access$000(SpmPastActivity.java:22)
at com.foong.spmsejerahscore.SpmPastActivity$1.onResponse(SpmPastActivity.java:59)
at com.foong.spmsejerahscore.SpmPastActivity$1.onResponse(SpmPastActivity.java:55)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
07-27 06:37:14.795 1591-2433/system_process W/ActivityManager: Force finishing activity com.foong.spmsejerahscore/.SpmPastActivity
07-27 06:37:14.825 1591-2433/system_process W/ActivityManager: Force finishing activity com.foong.spmsejerahscore/.MenuActivity
我遵循https://www.simplifiedcoding.net/android-volley-tutorial-to-get-json-from-server/的指南。 结果应显示列表视图。
答案 0 :(得分:0)
提到的url的StringRequest获得响应:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("dc7d8d143d0ed123d7b543cf7a2c48fd");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://www.spmsejarahscore.epizy.com/androidbackend/spmpast.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
因此,您将获得解析异常。尝试使用其他一些请求表单,例如JsonObjectRequest。
答案 1 :(得分:0)
我找到了两个可能存在错误的地方。首先,您没有在Volley请求字符串中提供方法类型。
StringRequest stringRequest = new StringRequest(Request.Method.GET,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.wtf("RESPONSE", response);
showJSON(response);
loading.setVisibility(View.INVISIBLE);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SpmPastActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
}
});
和第二个是解析时你必须提供数组名称。
JSONObject jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray("result");