我正在尝试从url解析json,但是我收到了一个错误。我的活动代码是这样的:
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(Activity.this);
// Set progressdialog title
//mProgressDialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Ké se kuece hoy...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
try {
// Locate the array name in JSON
//if(jsonobject == null)
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.my.url/file.json");
preversion_dos = jsonobject.getJSONObject("results");
version_definitiva_url = preversion_dos.getString(TAG_VERSION);
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
TextView dos=(TextView)findViewById(R.id.dos);
dos.setText(version_definitiva_url);
mProgressDialog.dismiss();
}
}
我的JSON Fuctions代码是这样的:
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
这个json我的编程方式是这样的:
sBuffer = new StringBuffer();
a= "{\"results\":[{\"version\":\""+version+"\"}]}";
appendEscaped(sBuffer, a);
System.out.println(sBuffer);
private static void appendEscaped( StringBuffer sBuffer, String str) {
char [] strChars = str.toCharArray();
for ( char c : strChars ) {
if ( c >= ' ' && c <= '~' && c != '/') {
// Character is ASCII, append as is
sBuffer.append(c);
} else {
sBuffer.append(String.format("\\u%04x", (int)c));
}
}
}
我得到的错误日志是这样的,它表示无法转换为JSONObject:
01-22 01:37:41.126: E/log_tag(4844): Error parsing data org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
01-22 01:37:41.132: E/AndroidRuntime(4844): FATAL EXCEPTION: AsyncTask #1
01-22 01:37:41.132: E/AndroidRuntime(4844): Process: com.iea.eee, PID: 4844
01-22 01:37:41.132: E/AndroidRuntime(4844): java.lang.RuntimeException: An error occured while executing doInBackground()
01-22 01:37:41.132: E/AndroidRuntime(4844): at android.os.AsyncTask$3.done(AsyncTask.java:304)
01-22 01:37:41.132: E/AndroidRuntime(4844): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-22 01:37:41.132: E/AndroidRuntime(4844): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-22 01:37:41.132: E/AndroidRuntime(4844): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-22 01:37:41.132: E/AndroidRuntime(4844): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-22 01:37:41.132: E/AndroidRuntime(4844): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-22 01:37:41.132: E/AndroidRuntime(4844): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-22 01:37:41.132: E/AndroidRuntime(4844): at java.lang.Thread.run(Thread.java:818)
01-22 01:37:41.132: E/AndroidRuntime(4844): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONObject.getJSONObject(java.lang.String)' on a null object reference
01-22 01:37:41.132: E/AndroidRuntime(4844): at com.iea.kesekuece.Comprobador$DownloadJSON.doInBackground(Comprobador.java:209)
01-22 01:37:41.132: E/AndroidRuntime(4844): at com.iea.kesekuece.Comprobador$DownloadJSON.doInBackground(Comprobador.java:1)
01-22 01:37:41.132: E/AndroidRuntime(4844): at android.os.AsyncTask$2.call(AsyncTask.java:292)
01-22 01:37:41.132: E/AndroidRuntime(4844): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-22 01:37:41.132: E/AndroidRuntime(4844): ... 4 more
01-22 01:37:41.362: E/WindowManager(4844): android.view.WindowLeaked: Activity com.iea.eee.Activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{3a1fdc61 V.E..... R......D 0,0-1026,348} that was originally added here
01-22 01:37:41.362: E/WindowManager(4844): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
01-22 01:37:41.362: E/WindowManager(4844): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
01-22 01:37:41.362: E/WindowManager(4844): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
01-22 01:37:41.362: E/WindowManager(4844): at android.app.Dialog.show(Dialog.java:298)
01-22 01:37:41.362: E/WindowManager(4844): at com.iea.kesekuece.Comprobador$DownloadJSON.onPreExecute(Comprobador.java:194)
01-22 01:37:41.362: E/WindowManager(4844): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591)
01-22 01:37:41.362: E/WindowManager(4844): at android.os.AsyncTask.execute(AsyncTask.java:539)
01-22 01:37:41.362: E/WindowManager(4844): at com.iea.kesekuece.Comprobador.onCreate(Comprobador.java:76)
01-22 01:37:41.362: E/WindowManager(4844): at android.app.Activity.performCreate(Activity.java:5990)
01-22 01:37:41.362: E/WindowManager(4844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
01-22 01:37:41.362: E/WindowManager(4844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
01-22 01:37:41.362: E/WindowManager(4844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
01-22 01:37:41.362: E/WindowManager(4844): at android.app.ActivityThread.access$900(ActivityThread.java:154)
01-22 01:37:41.362: E/WindowManager(4844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
01-22 01:37:41.362: E/WindowManager(4844): at android.os.Handler.dispatchMessage(Handler.java:102)
01-22 01:37:41.362: E/WindowManager(4844): at android.os.Looper.loop(Looper.java:135)
01-22 01:37:41.362: E/WindowManager(4844): at android.app.ActivityThread.main(ActivityThread.java:5294)
01-22 01:37:41.362: E/WindowManager(4844): at java.lang.reflect.Method.invoke(Native Method)
01-22 01:37:41.362: E/WindowManager(4844): at java.lang.reflect.Method.invoke(Method.java:372)
01-22 01:37:41.362: E/WindowManager(4844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
01-22 01:37:41.362: E/WindowManager(4844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
答案 0 :(得分:0)
您没有收到JSON
回复,因为您可以在logcat
中看到它,但您也会收到一些标记<html><body><script
data org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONObject
答案 1 :(得分:0)
我的JavaScript错误。
<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("c7e81067624ba31b7ca1a73062e4ca4a");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.href="http://kese.260mb.net/hola/version.json?ckattempt=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>
我已在POSTMAN中测试过,如果我直接在chrome中打开此链接(启用了java脚本),它将完美运行,但我使用POSTMAN来运行发生Java脚本错误的地方。
您必须检查启用与否的 JavaScript 错误。
答案 2 :(得分:0)
我认为文件json有问题。
为什么不使用Volley库解析json,它简单快捷。