Java org.json.JSONException:状态没有值
org.json.JSONException:状态无值 下面是我的json解析java代码方法 爪哇Java org.json.JSONException:状态没有值,java,php,android,json,Java,Php,Android,Json,org.json.JSONException:状态无值 下面是我的json解析java代码方法 爪哇 public void performSearch(){ 字符串url=”http://192.168.0.136/fyp/stitle1.php"; RequestQueue RequestQueue=Volley.newRequestQueue(Stitle.this); JsonObjectRequest jsObjRequest=新的JsonObjectRequest(Request.M
public void performSearch(){
字符串url=”http://192.168.0.136/fyp/stitle1.php";
RequestQueue RequestQueue=Volley.newRequestQueue(Stitle.this);
JsonObjectRequest jsObjRequest=新的JsonObjectRequest(Request.Method.POST,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(“Response”,Response.toString());
试一试{
//将字符串转换为json数组对象
JSONObject数组=新的JSONObject();
//Log.i(“测试”,“值:”+array.getString(“状态”));
Log.i(“测试”,“值:”+response.getString(“状态”));
if(array.getString(“status”).equals(“true”)){
JSONArray JSONArray=array.getJSONArray(“搜索”);
Log.i(“测试”,“值:”+数组);
for(int i=0;i
将参数传递给android jsonobject的Php文件
stitle1.php
php
当我运行应用程序时,这些行出现在logcat面板上
I/测试:值:4
I/test:org.json.JSONException:status没有值
logcat上显示两行,表示参数未正确发送的错误
我们将一步一步走下去。让我们从回应开始 您的PHP代码正在返回一个
状态
值=4,这表明您没有正确获取发送到PHP代码的参数。甚至可能没有调用getParams()
将getParams()
方法更改为如下所示:
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
try{
String s = searchtitle.getText().toString();
Log.e("Volley request", "getParams called : " + s);
params.put("Title", s);
}
catch(Exception ex){
Log.e("Volley request ERROR", ex.getMessage());
}
return params;
}
现在要解析JSONObject:
Map<String, String> booksMap = new HashMap<>();
private void parseJsonObject(JSONObject jsonObject){
try{
if(jsonObject == null) return;
//Not Available!
String na = "NA"
Log.i("test", " value : " + jsonObject.toString());
if(jsonObject.has("books")){
JSONArray array = jsonObject.getJSONArray("books");
for(int i = 0; array.length(); i++){
JSONObject book = array.getJSONObject(i);
Iterator<String> it = book.keys();
while(it.hasNext()){
String key = it.next();
String value = book.optString(key, na);
booksMap.put(key, value);
}
}
}
}
catch(Exception ex){
Log.e(TAG, ex.getMessage());
}
}
Map booksMap=newhashmap();
私有void parseJsonObject(JSONObject JSONObject){
试一试{
if(jsonObject==null)返回;
//不可用!
字符串na=“na”
Log.i(“test”,“value:+jsonObject.toString());
如果(jsonObject.has(“账簿”)){
JSONArray数组=jsonObject.getJSONArray(“books”);
对于(int i=0;array.length();i++){
JSONObject book=array.getJSONObject(i);
迭代器it=book.keys();
while(it.hasNext()){
String key=it.next();
字符串值=book.optString(键,na);
booksMap.put(键、值);
}
}
}
}
捕获(例外情况除外){
Log.e(标记,例如getMessage());
}
}
由于此行,您将收到错误:如果(array.getString(“status”).equals(“true”){
::您仍在询问array
哪个是空的。您需要response.getString(…
使用response之后,现在开始这些。get…I/test:value:4 else error是的,此代码不尝试处理错误。不要担心错误。POST参数很重要。使用上面的代码,查看日志中显示的有关参数“Volley request”的内容显示了吗?同样的错误I/test:value:4-----否则errorBTW:“TAG”是一个类变量,看起来像这样::private static final String TAG=YourActivity.class.getSimpleName()
请注意,您需要将YourActivity
更改为实际类名的类名。@AsadMallick::查看对答案的更改。您可以使用此代码,错误就会消失。让我知道这是否可以消除错误。@AsadMallick::您有机会看一下c吗
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
try{
String s = searchtitle.getText().toString();
Log.e("Volley request", "getParams called : " + s);
params.put("Title", s);
}
catch(Exception ex){
Log.e("Volley request ERROR", ex.getMessage());
}
return params;
}
@Override
public void onResponse(JSONObject response) {
// Log.d("Response", response.toString());
try {
//converting the string to json array object
if(response != null){
if(!response.has("status"){
Log.e(TAG, "Something went wrong -- no status key!");
return;
}
else{
int status = response.optInt("status", -1);
if(status == 1){
//There could be quite a few books in this response...
//...you might want to parse in an AsyncTask instead
parseJsonObject(response);
}
else{
String message = response.optString("message", "uups");
Log.e(TAG, "error message = " + message);
return;
}
}
}
}
catch(Exception ex){
Log.e(TAG, ex.getMessage());
}
}
Map<String, String> booksMap = new HashMap<>();
private void parseJsonObject(JSONObject jsonObject){
try{
if(jsonObject == null) return;
//Not Available!
String na = "NA"
Log.i("test", " value : " + jsonObject.toString());
if(jsonObject.has("books")){
JSONArray array = jsonObject.getJSONArray("books");
for(int i = 0; array.length(); i++){
JSONObject book = array.getJSONObject(i);
Iterator<String> it = book.keys();
while(it.hasNext()){
String key = it.next();
String value = book.optString(key, na);
booksMap.put(key, value);
}
}
}
}
catch(Exception ex){
Log.e(TAG, ex.getMessage());
}
}