问题描述
我有以下code的一个问题。我分析从数据库到Android的JSON字符串,但我似乎无法得到code运行。
我用的AsyncTask
类 doInBackground
来检索 JSONparse
从数据库中,并把它给了 onPostExecute
方法。
我想给这些参数的setCharges方法来获取参数并设置收费到Android TextView的双倍。
请指教如果有什么不对我的code。
先谢谢了。
的code以下是PHP脚本
< PHP要求(config.php文件); //初始查询
$查询=选择fHAH,沙阿rHAH FROM费;//执行查询$语句= $ DB-GT&; prepare($查询);
$结果= $ stmt->执行($ query_params);//最后,我们可以检索所有找到的行成使用使用fetchall数组
$行= $ stmt->使用fetchall();
如果($行){ $响应[费用] =阵列(); 的foreach($行作为$行){ $费=阵列(); $收费[fHAH] = $行[fHAH];
$收费[沙赫] = $行[沙赫];
$收费[rHAH] = $行[rHAH];
//更新我们的repsonse JSON数据
array_push($响应[费用],$后);
} //呼应JSON响应
回声json_en code($响应);
}
这是AsyncTask的类
私有类getLoadData扩展的AsyncTask<太虚,太虚,太虚> { @覆盖
保护无效doInBackground(虚空...... PARAMS){ JSONParser jParser =新JSONParser();
JSON = jParser.getJSONDataFromURL(chargesSettingURL); 返回null;
} @覆盖
保护无效onPostExecute(虚空结果){
super.onPostExecute(结果); 双fCharge = 0.0;
双sCharge = 0.0;
双rCharge = 0.0; 尝试{ chargesJSONArray = json.getJSONArray(TAG_CHARGES); 的for(int i = 0; I< chargesJSONArray.length();我++){
JSONObject的C = chargesJSONArray.getJSONObject(I) fCharge = Double.parseDouble(c.getString(TAG_FHAH));
sCharge = Double.parseDouble(c.getString(TAG_SHAH));
rCharge = Double.parseDouble(c.getString(TAG_RHAH));
setCharges(fCharge,sCharge,rCharge); } }
赶上(JSONException E){
e.printStackTrace();
} }
}
这是setCharges方法
公共无效setCharges(双fHAH,双沙,双rHAH){
this.F_HALF_AN_HOUR = fHAH;
this.S_HALF_AN_HOUR = SHAH;
this.R_HALF_AN_HOURS = rHAH;
}
我已经包含错误日志
17 08-09:56:35.650 2060至94年/ com.gpark.nerdherd.fypE / AndroidRuntime:致命的异常:AsyncTask的#1
工艺:com.gpark.nerdherd.fyp,PID:1860
了java.lang.RuntimeException:执行doInBackground发生错误()
在android.os.AsyncTask $ 3.done(AsyncTask.java:300)
在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
在java.util.concurrent.FutureTask.setException(FutureTask.java:222)
在java.util.concurrent.FutureTask.run(FutureTask.java:242)
在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)
在java.lang.Thread.run(Thread.java:818)
显示java.lang.NullPointerException:引起试图调用虚拟方法java.lang.String中java.lang.StringBuilder.toString()在空对象引用
在com.gpark.nerdherd.fyp.libs.JSONParser.getJSONDataFromURL(JSONParser.java:45)
在com.gpark.nerdherd.fyp.libs.CalculateParking $ getLoadData.doInBackground(CalculateParking.java:42)
在com.gpark.nerdherd.fyp.libs.CalculateParking $ getLoadData.doInBackground(CalculateParking.java:36)
在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
在java.util.concurrent.FutureTask.run(FutureTask.java:237)
在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)
在java.lang.Thread.run(Thread.java:818)
08-09 17:56:43.620 2110年至2110年/ com.gpark.nerdherd.fyp D /图集:验证地图...
08-09 17:56:43.980 2110至2125年/ com.gpark.nerdherd.fyp D /:HostConnection ::得到()新主机连接建立0x7fd75984f1a0,TID 2125
08-09 17:56:44.000 2110至2125年/ com.gpark.nerdherd.fyp I / OpenGLRenderer:初始化EGL,1.4版本
08-09 17:56:44.060 2110至2125年/ com.gpark.nerdherd.fyp D / OpenGLRenderer:启用调试模式0
08-09 17:56:44.070 2110至2125年/ com.gpark.nerdherd.fyp W / EGL_emulation:eglSurfaceAttrib没有实现
08-09 17:56:44.070 2110至2125年/ com.gpark.nerdherd.fyp W / OpenGLRenderer:无法设置EGL_SWAP_BEHAVIOR表面0x7fd75984e300错误= EGL_SUCCESS
08-09 17:56:47.150 2110至2129年/ com.gpark.nerdherd.fyp E / AndroidRuntime:致命的异常:AsyncTask的#1
工艺:com.gpark.nerdherd.fyp,PID:2110
了java.lang.RuntimeException:执行doInBackground发生错误()
在android.os.AsyncTask $ 3.done(AsyncTask.java:300)
在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
在java.util.concurrent.FutureTask.setException(FutureTask.java:222)
在java.util.concurrent.FutureTask.run(FutureTask.java:242)
在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)
在java.lang.Thread.run(Thread.java:818)
显示java.lang.NullPointerException:引起试图调用虚拟方法java.lang.String中java.lang.StringBuilder.toString()在空对象引用
在com.gpark.nerdherd.fyp.libs.JSONParser.getJSONDataFromURL(JSONParser.java:45)
在com.gpark.nerdherd.fyp.libs.CalculateParking $ getLoadData.doInBackground(CalculateParking.java:42)
在com.gpark.nerdherd.fyp.libs.CalculateParking $ getLoadData.doInBackground(CalculateParking.java:36)
在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
在java.util.concurrent.FutureTask.run(FutureTask.java:237)
在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)
在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)
在java.lang.Thread.run(Thread.java:818)
08-09 17:56:47.270 2110年至2133年/ com.gpark.nerdherd.fyp I /工艺:发送信号。 PID:2110 SIG:9
此的getJSONDataFromURL
公开的JSONObject getJSONDataFromURL(最终字符串URL){ 尝试{ urlObj =新的URL(网址);
HttpURLConnection的=(HttpURLConnection类)urlObj.openConnection();
httpURLConnection.setDoOutput(真);
httpURLConnection.setRequestMethod(POST);
httpURLConnection.setRequestProperty(接收字符集,字符集);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.connect();
paramsString = sbParams.toString(); WR =新的DataOutputStream类(httpURLConnection.getOutputStream());
wr.writeBytes(paramsString);
wr.flush();
wr.close(); }赶上(IOException异常五){
e.printStackTrace();
} 尝试{
//接收来自服务器的响应
在的InputStream =新的BufferedInputStream(httpURLConnection.getInputStream());
的BufferedReader读者=新的BufferedReader(新InputStreamReader的(在ISO-8859-1),8); 串线;
而((行= reader.readLine())!= NULL){
result.append(线);
} 附寄();
JSON = result.toString(); }赶上(IOException异常五){
Log.e(JSON分析器,结果:+ e.toString());
} httpURLConnection.disconnect(); //尝试分析字符串到一个JSON对象
尝试{
jObj =新的JSONObject(JSON);
}赶上(JSONException E){
Log.e(JSON解析器,错误分析数据+ e.toString());
} //返回JSON对象
返回jObj;}
您似乎在PHP code发明了 $岗位
变量。另外你似乎是在复杂的事情
试试这个
要求(config.php文件);
//初始查询
$查询=选择fHAH,沙阿rHAH FROM费;$响应=阵列();//因为没有什么prepare使用 - >查询
的foreach($ DB-GT&;查询($ SQL)为$行) $响应[费用] [] = $行;
}回声json_en code($响应);
现在你有一个正确的JSON对象在java code处理Android设备上
现在,如果你
I have an issue with the code below. I parsed the JSON string from the database to the android but I can't seem to get the code running.
I used the asynctask
class to doInBackground
to retrieve the JSONparse
from the database and gave it to onPostExecute
method.
I'd like to give the parameters to the setCharges method to get the parameters and sets the charges to the android textview as double.
Please advice if anything is wrong with my code.
Thanks in advance.
The code below is the PHP script
<?php
require("config.php");
//initial query
$query = "SELECT fHAH,sHAH,rHAH FROM charges";
//execute query
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
if ($rows) {
$response["charges"] = array();
foreach ($rows as $row) {
$charge = array();
$charge["fHAH"] = $row["fHAH"];
$charge["sHAH"] = $row["sHAH"];
$charge["rHAH"] = $row["rHAH"];
//update our repsonse JSON data
array_push($response["charges"], $post);
}
// echoing JSON response
echo json_encode($response);
}
This is the AsyncTask Class
private class getLoadData extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
JSONParser jParser = new JSONParser();
json = jParser.getJSONDataFromURL(chargesSettingURL);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
double fCharge = 0.0;
double sCharge = 0.0;
double rCharge = 0.0;
try {
chargesJSONArray = json.getJSONArray(TAG_CHARGES);
for (int i = 0; i < chargesJSONArray.length(); i++) {
JSONObject c = chargesJSONArray.getJSONObject(i);
fCharge = Double.parseDouble(c.getString(TAG_FHAH));
sCharge = Double.parseDouble(c.getString(TAG_SHAH));
rCharge = Double.parseDouble(c.getString(TAG_RHAH));
setCharges(fCharge, sCharge, rCharge);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
And this is the setCharges method
public void setCharges(double fHAH, double sHAH, double rHAH) {
this.F_HALF_AN_HOUR = fHAH;
this.S_HALF_AN_HOUR = sHAH;
this.R_HALF_AN_HOURS = rHAH;
}
I've included the error log
08-09 17:56:35.650 1860-2094/com.gpark.nerdherd.fyp
E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.gpark.nerdherd.fyp, PID: 1860
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.StringBuilder.toString()' on a null object reference
at com.gpark.nerdherd.fyp.libs.JSONParser.getJSONDataFromURL(JSONParser.java:45)
at com.gpark.nerdherd.fyp.libs.CalculateParking$getLoadData.doInBackground(CalculateParking.java:42)
at com.gpark.nerdherd.fyp.libs.CalculateParking$getLoadData.doInBackground(CalculateParking.java:36)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
08-09 17:56:43.620 2110-2110/com.gpark.nerdherd.fyp D/Atlas﹕ Validating map...
08-09 17:56:43.980 2110-2125/com.gpark.nerdherd.fyp D/﹕ HostConnection::get() New Host Connection established 0x7fd75984f1a0, tid 2125
08-09 17:56:44.000 2110-2125/com.gpark.nerdherd.fyp I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-09 17:56:44.060 2110-2125/com.gpark.nerdherd.fyp D/OpenGLRenderer﹕ Enabling debug mode 0
08-09 17:56:44.070 2110-2125/com.gpark.nerdherd.fyp W/EGL_emulation﹕ eglSurfaceAttrib not implemented
08-09 17:56:44.070 2110-2125/com.gpark.nerdherd.fyp W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fd75984e300, error=EGL_SUCCESS
08-09 17:56:47.150 2110-2129/com.gpark.nerdherd.fyp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.gpark.nerdherd.fyp, PID: 2110
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.StringBuilder.toString()' on a null object reference
at com.gpark.nerdherd.fyp.libs.JSONParser.getJSONDataFromURL(JSONParser.java:45)
at com.gpark.nerdherd.fyp.libs.CalculateParking$getLoadData.doInBackground(CalculateParking.java:42)
at com.gpark.nerdherd.fyp.libs.CalculateParking$getLoadData.doInBackground(CalculateParking.java:36)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
08-09 17:56:47.270 2110-2133/com.gpark.nerdherd.fyp I/Process﹕ Sending signal. PID: 2110 SIG: 9
This the the getJSONDataFromURL
public JSONObject getJSONDataFromURL(final String url) {
try {
urlObj = new URL(url);
httpURLConnection = (HttpURLConnection) urlObj.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept-Charset", charset);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.connect();
paramsString = sbParams.toString();
wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(paramsString);
wr.flush();
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
//Receive the response from the server
InputStream in = new BufferedInputStream(httpURLConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "iso-8859-1"), 8);
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
in.close();
json = result.toString();
} catch (IOException e) {
Log.e("JSON Parser", "result: " + e.toString());
}
httpURLConnection.disconnect();
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Object
return jObj;
}
You seem to have invented a $post
variable in your PHP code. Also you seem to be over complicating things
Try this
require("config.php");
//initial query
$query = "SELECT fHAH,sHAH,rHAH FROM charges";
$response = array();
// as there is nothing to prepare use ->query
foreach ($db->query($sql) as $row)
$response["charges"][] = $row;
}
echo json_encode($response);
Now you have a proper json object to process in the java code on the android device
Now if you read this you will find out how to parse a JSON string into a JSON object in JAVA
这篇关于如何解析来自的AsyncTask的PARAMS到方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!