JSONObject无法转换为JSONArray请帮助 [英] JSONObject cannot be converted to JSONArray Please help

查看:123
本文介绍了JSONObject无法转换为JSONArray请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,
这是我的第一次JSON-PHP经验.我正在开发一个小型应用程序,以从在线数据库中获取用户信息.用户输入用户名和密码,然后按提交按钮.因此应用程序应该能够检查该用户是否可用.

我有一个例外,说
无法将org.json.JSONObject $ 1类型的值null转换为JSONArray"

这是我的PHP脚本

dear friends,
this is my first JSON-PHP experience.i''m developing a small application to get user information from an online database. User enters his username and password then press submit button.So application should be able to check whether that user is available or not.

I''m getting an exception saying
"Value null of type org.json.JSONObject$1 cannot be converted to JSONArray"

here is my php script

<?php

mysql_connect("localhost","tadmin","t2012");
mysql_select_db("tdb");


$sql=mysql_query("SELECT uid,uname FROM tuser WHERE uname ='".$_REQUEST['uname']."' AND password ='".$_REQUEST['password']."'");


while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));

mysql_close();

?>



这是我的android代码



this is my android code

package com.drock.tbnote;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.nio.Buffer;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class incui extends Activity {

private Vibrator Myvib;
private InputStream is; //input steram
String result = ""; //result

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.incui);

Button btnsubmit = (Button) findViewById(R.id.button1);
Button btnview = (Button) findViewById(R.id.button2);
EditText txtuname = (EditText) findViewById(R.id.editText1);
EditText txtpass = (EditText) findViewById(R.id.editText2);
final TextView lbluid = (TextView) findViewById(R.id.textView5);
final TextView lbluname = (TextView) findViewById(R.id.textView6);

        
//creating array list
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("uname",txtuname.getText().toString()));	        
nameValuePairs.add(new BasicNameValuePair("password", txtpass.getText().toString()));
	        
	        
Myvib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
//Button click event	        
btnsubmit.setOnClickListener(new OnClickListener() {
				
public void onClick(View v) {
Myvib.vibrate(50);
			
					
//connecting http web server
try{
			
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.mysite.org/php/getuser.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();		
				
						
}catch(Exception e1){
Log.e("http", e1.getMessage());
Toast toast=Toast.makeText(getApplicationContext(),"http error: "+e1.getMessage(), Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
					
//reading from server
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.toString() + "\n");
			 
}
is.close();
result = sb.toString();
						
}catch(Exception e1){
Log.e("http", e1.getMessage());
Toast toast=Toast.makeText(getApplicationContext(),"read error: "+e1.getMessage(), Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
	
//parsing JSON data
int usrid;
String usrname;
try{
		
JSONArray jArray = new JSONArray(result);
JSONObject json_data = new JSONObject();
for(int i=0;i<jArray.length();i++){
	json_data = jArray.getJSONObject(i);
	usrid = json_data.getInt("uid");
	usrname = json_data.getString("uname");
}
Toast toast=Toast.makeText(getApplicationContext(),"Authenticated successfuly", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
		
		
}catch(Exception e1){
Log.e("http", e1.getMessage());
Toast toast=Toast.makeText(getApplicationContext(),"json error: "+e1.getMessage(), Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}

					
					
	
}
});

}

}



有人可以帮我解决我的问题吗?谢谢..



can someone please help me to solve my problem?? thanks..

推荐答案

1无法转换为JSONArray"

这是我的PHP脚本

1 cannot be converted to JSONArray"

here is my php script

<?php

mysql_connect("localhost","tadmin","t2012");
mysql_select_db("tdb");


sql = mysql_query( " .
sql=mysql_query("SELECT uid,uname FROM tuser WHERE uname ='".


_REQUEST [' uname']." 'AND password ='".
_REQUEST['uname']."' AND password ='".


这篇关于JSONObject无法转换为JSONArray请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆