JSON对象与Android整合 [英] Json object integrate with android
问题描述
我已经使用这个code的 http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ 的GCM。但有每当我点击查看或创建新的产品button.I麻烦已经检查所教程描述如下,但这并不解决我的问题的解决方案。这里是我的
20 02-14:06:09.775:W / KeyCharacterMap(738):无键盘ID 0
02-14 20:06:09.775:W / KeyCharacterMap(738):使用默认的键盘对应:/system/usr/keychars/qwerty.kcm.bin
02-14 20:06:13.105:E / JSON解析器(738):错误分析数据org.json.JSONException:价值与LT; java.lang.String类型的DOCTYPE不能转换到的JSONObject
02-14 20:06:13.105:W / dalvikvm(738):主题ID = 9:螺纹未捕获的异常(组= 0x40015560)退出
02-14 20:06:13.115:E / AndroidRuntime(738):致命异常:AsyncTask的#1
02-14 20:06:13.115:E / AndroidRuntime(738):了java.lang.RuntimeException:执行doInBackground发生错误()
02-14 20:06:13.115:E / AndroidRuntime(738):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
02-14 20:06:13.115:E / AndroidRuntime(738):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:274)
02-14 20:06:13.115:E / AndroidRuntime(738):在java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-14 20:06:13.115:E / AndroidRuntime(738):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:308)
02-14 20:06:13.115:E / AndroidRuntime(738):在java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-14 20:06:13.115:E / AndroidRuntime(738):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-14 20:06:13.115:E / AndroidRuntime(738):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:581)
02-14 20:06:13.115:E / AndroidRuntime(738):在java.lang.Thread.run(Thread.java:1019)
02-14 20:06:13.115:E / AndroidRuntime(738):致:显示java.lang.NullPointerException
02-14 20:06:13.115:E / AndroidRuntime(738):在com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:130)
02-14 20:06:13.115:E / AndroidRuntime(738):在com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
02-14 20:06:13.115:E / AndroidRuntime(738):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
02-14 20:06:13.115:E / AndroidRuntime(738):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:306)
02-14 20:06:13.115:E / AndroidRuntime(738):... 4个
02-14 20:06:14.213:E /窗口管理器(738):活动com.example.androidhive.AllProductsActivity渗漏窗口com.android.internal.policy.impl.PhoneWindow$DecorView@405375e0,最初这里加入
02-14 20:06:14.213:E /窗口管理器(738):android.view.WindowLeaked:活动com.example.androidhive.AllProductsActivity渗漏窗口com.android.internal.policy.impl.PhoneWindow$DecorView@405375e0这是原本在这里添加
02-14 20:06:14.213:E /窗口管理器(738):在android.view.ViewRoot<&初始化GT;(ViewRoot.java:258)
02-14 20:06:14.213:E /窗口管理器(738):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-14 20:06:14.213:E /窗口管理器(738):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-14 20:06:14.213:E /窗口管理器(738):在android.view.Window $ LocalWindowManager.addView(Window.java:424)
02-14 20:06:14.213:E /窗口管理器(738):在android.app.Dialog.show(Dialog.java:241)
02-14 20:06:14.213:E /窗口管理器(738):在com.example.androidhive.AllProductsActivity$LoadAllProducts.on$p$pExecute(AllProductsActivity.java:117)
02-14 20:06:14.213:E /窗口管理器(738):在android.os.AsyncTask.execute(AsyncTask.java:391)
02-14 20:06:14.213:E /窗口管理器(738):在com.example.androidhive.AllProductsActivity.onCreate(AllProductsActivity.java:57)
02-14 20:06:14.213:E /窗口管理器(738):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 20:06:14.213:E /窗口管理器(738):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-14 20:06:14.213:E /窗口管理器(738):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-14 20:06:14.213:E /窗口管理器(738):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
02-14 20:06:14.213:E /窗口管理器(738):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
02-14 20:06:14.213:E /窗口管理器(738):在android.os.Handler.dispatchMessage(Handler.java:99)
02-14 20:06:14.213:E /窗口管理器(738):在android.os.Looper.loop(Looper.java:123)
02-14 20:06:14.213:E /窗口管理器(738):在android.app.ActivityThread.main(ActivityThread.java:3683)
02-14 20:06:14.213:E /窗口管理器(738):在java.lang.reflect.Method.invokeNative(本机方法)
02-14 20:06:14.213:E /窗口管理器(738):在java.lang.reflect.Method.invoke(Method.java:507)
02-14 20:06:14.213:E /窗口管理器(738):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
02-14 20:06:14.213:E /窗口管理器(738):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-14 20:06:14.213:E /窗口管理器(738):在dalvik.system.NativeStart.main(本机方法)
02-14 20:06:25.245:I /流程(738):发送信号。 PID:738 SIG:9
下面是我的AsyncTask code ---
/ **
*背景异步任务以通过HTTP请求负载的所有产品
* * /
类LoadAllProducts扩展的AsyncTask<字符串,字符串,字符串> { / **
*启动后台线程显示进度对话框之前
* * /
@覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(AllProductsActivity.this);
pDialog.setMessage(加载产品请稍候......);
pDialog.setIndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
} / **
*让所有的产品网址
* * /
保护字符串doInBackground(字符串参数... args){
//大厦参数
清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
// URL从获取JSON字符串
JSONObject的JSON = jParser.makeHtt prequest(url_all_products,GET,则params); //检查JSON效应初探日志的猫
Log.d(所有产品,json.toString()); 尝试{
//检查成功TAG
INT成功= json.getInt(TAG_SUCCESS); 如果(成功== 1){
发现//产品
//获取产品阵列
产品= json.getJSONArray(TAG_PRODUCTS); //产品通过所有循环
的for(int i = 0; I< products.length();我++){
JSONObject的C = products.getJSONObject(I) //存储在变量中的每个JSON项目
字符串ID = c.getString(TAG_PID);
字符串名称= c.getString(TAG_NAME); //创建新的HashMap
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); //将每个子节点的HashMap键=>值
map.put(TAG_PID,身份证);
map.put(TAG_NAME,名); //添加HashList到ArrayList的
productsList.add(地图);
}
}其他{
//没有发现产品
//启动添加新产品活动
意图I =新意图(getApplicationContext()
NewProductActivity.class);
//关闭所有previous活动
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ⅰ);
}
}赶上(JSONException E){
e.printStackTrace();
} 返回null;
} / **
*在完成后台任务之后辞退进度对话框
* ** /
保护无效onPostExecute(字符串FILE_URL){
//让所有的产品后,关闭该对话框
pDialog.dismiss();
//从后台线程更新界面
runOnUiThread(新的Runnable(){
公共无效的run(){
/ **
*更新解析JSON数据到ListView控件
* * /
ListAdapter适配器=新SimpleAdapter(
AllProductsActivity.this,产品列表,
R.layout.list_item,新的String [] {TAG_PID,
TAG_NAME},
新的INT [] {R.id.pid,R.id.name});
//更新列表视图
setListAdapter(适配器);
}
}); } }
做阿宾说以后的事情......错误的长度减少了.....现在只具有这个错误 -
20 02-14:39:06.094:E / JSON解析器(1182):错误分析数据org.json.JSONException:值< java.lang.String类型的DOCTYPE!不能转换到的JSONObject
02-14 20:39:06.094:W / dalvikvm(1182):主题ID = 9:螺纹未捕获的异常(组= 0x40015560)退出
02-14 20:39:06.104:E / AndroidRuntime(1182):致命异常:AsyncTask的#1
02-14 20:39:06.104:E / AndroidRuntime(1182):了java.lang.RuntimeException:执行doInBackground发生错误()
02-14 20:39:06.104:E / AndroidRuntime(1182):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
02-14 20:39:06.104:E / AndroidRuntime(1182):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:274)
02-14 20:39:06.104:E / AndroidRuntime(1182):在java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-14 20:39:06.104:E / AndroidRuntime(1182):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:308)
02-14 20:39:06.104:E / AndroidRuntime(1182):在java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-14 20:39:06.104:E / AndroidRuntime(1182):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-14 20:39:06.104:E / AndroidRuntime(1182):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:581)
02-14 20:39:06.104:E / AndroidRuntime(1182):在java.lang.Thread.run(Thread.java:1019)
02-14 20:39:06.104:E / AndroidRuntime(1182):因:显示java.lang.NullPointerException
02-14 20:39:06.104:E / AndroidRuntime(1182):在com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:137)
02-14 20:39:06.104:E / AndroidRuntime(1182):在com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
02-14 20:39:06.104:E / AndroidRuntime(1182):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
02-14 20:39:06.104:E / AndroidRuntime(1182):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:306)
02-14 20:39:06.104:E / AndroidRuntime(1182):... 4个
下面是主要的错误:
产生的原因:在显示java.lang.NullPointerException
com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:130)
无论是在你的doInBackground方法行130中发生的导致空指针。
请在code指出哪一行是130。
I have used this code http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ for GCM . But having trouble whenever i click to view or create a new produce button.I have checked the solution that is described below the tutorial but that doesn't solve my problem. here is my
02-14 20:06:09.775: W/KeyCharacterMap(738): No keyboard for id 0
02-14 20:06:09.775: W/KeyCharacterMap(738): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-14 20:06:13.105: E/JSON Parser(738): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-14 20:06:13.105: W/dalvikvm(738): threadid=9: thread exiting with uncaught exception (group=0x40015560)
02-14 20:06:13.115: E/AndroidRuntime(738): FATAL EXCEPTION: AsyncTask #1
02-14 20:06:13.115: E/AndroidRuntime(738): java.lang.RuntimeException: An error occured while executing doInBackground()
02-14 20:06:13.115: E/AndroidRuntime(738): at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.lang.Thread.run(Thread.java:1019)
02-14 20:06:13.115: E/AndroidRuntime(738): Caused by: java.lang.NullPointerException
02-14 20:06:13.115: E/AndroidRuntime(738): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:130)
02-14 20:06:13.115: E/AndroidRuntime(738): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
02-14 20:06:13.115: E/AndroidRuntime(738): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-14 20:06:13.115: E/AndroidRuntime(738): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-14 20:06:13.115: E/AndroidRuntime(738): ... 4 more
02-14 20:06:14.213: E/WindowManager(738): Activity com.example.androidhive.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405375e0 that was originally added here
02-14 20:06:14.213: E/WindowManager(738): android.view.WindowLeaked: Activity com.example.androidhive.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405375e0 that was originally added here
02-14 20:06:14.213: E/WindowManager(738): at android.view.ViewRoot.<init>(ViewRoot.java:258)
02-14 20:06:14.213: E/WindowManager(738): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-14 20:06:14.213: E/WindowManager(738): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-14 20:06:14.213: E/WindowManager(738): at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-14 20:06:14.213: E/WindowManager(738): at android.app.Dialog.show(Dialog.java:241)
02-14 20:06:14.213: E/WindowManager(738): at com.example.androidhive.AllProductsActivity$LoadAllProducts.onPreExecute(AllProductsActivity.java:117)
02-14 20:06:14.213: E/WindowManager(738): at android.os.AsyncTask.execute(AsyncTask.java:391)
02-14 20:06:14.213: E/WindowManager(738): at com.example.androidhive.AllProductsActivity.onCreate(AllProductsActivity.java:57)
02-14 20:06:14.213: E/WindowManager(738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-14 20:06:14.213: E/WindowManager(738): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 20:06:14.213: E/WindowManager(738): at android.os.Looper.loop(Looper.java:123)
02-14 20:06:14.213: E/WindowManager(738): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-14 20:06:14.213: E/WindowManager(738): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 20:06:14.213: E/WindowManager(738): at java.lang.reflect.Method.invoke(Method.java:507)
02-14 20:06:14.213: E/WindowManager(738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-14 20:06:14.213: E/WindowManager(738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-14 20:06:14.213: E/WindowManager(738): at dalvik.system.NativeStart.main(Native Method)
02-14 20:06:25.245: I/Process(738): Sending signal. PID: 738 SIG: 9
Here is my asynctask code ---
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllProductsActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
after doing the thing that ben has said ...the length of error has reduced ..... now having only this error --
02-14 20:39:06.094: E/JSON Parser(1182): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-14 20:39:06.094: W/dalvikvm(1182): threadid=9: thread exiting with uncaught exception (group=0x40015560)
02-14 20:39:06.104: E/AndroidRuntime(1182): FATAL EXCEPTION: AsyncTask #1
02-14 20:39:06.104: E/AndroidRuntime(1182): java.lang.RuntimeException: An error occured while executing doInBackground()
02-14 20:39:06.104: E/AndroidRuntime(1182): at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.lang.Thread.run(Thread.java:1019)
02-14 20:39:06.104: E/AndroidRuntime(1182): Caused by: java.lang.NullPointerException
02-14 20:39:06.104: E/AndroidRuntime(1182): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:137)
02-14 20:39:06.104: E/AndroidRuntime(1182): at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
02-14 20:39:06.104: E/AndroidRuntime(1182): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-14 20:39:06.104: E/AndroidRuntime(1182): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-14 20:39:06.104: E/AndroidRuntime(1182): ... 4 more
Here is the main error:
Caused by: java.lang.NullPointerException at
com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:130)
Whatever is happening at line 130 in your doInBackground method is causing a nullPointer.
Please point out which line is 130 in your code.
这篇关于JSON对象与Android整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!