Click here to Skip to main content
11,922,381 members (31,772 online)
Rate this:
Please Sign up or sign in to vote.
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


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


this is my android code

package com.drock.tbnote;
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.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

public void onCreate(Bundle savedInstanceState) {
Button btnsubmit = (Button) findViewById(;
Button btnview = (Button) findViewById(;
EditText txtuname = (EditText) findViewById(;
EditText txtpass = (EditText) findViewById(;
final TextView lbluid = (TextView) findViewById(;
final TextView lbluname = (TextView) findViewById(;
//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) {
//connecting http web server
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("");
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);;
//reading from server
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");
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);;
//parsing JSON data
int usrid;
String usrname;
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);;
}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);;

can someone please help me to solve my problem?? thanks..
Posted 20-May-12 3:23am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Connect Android application with MySql Databse using PHP

Try this tutorials..

Click Here Tutorial 1

Click Here Tutorial 2

Click Here Tutorial 3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 453
1 Dave Kreskowiak 335
2 Sergey Alexandrovich Kryukov 263
3 Richard MacCutchan 225
4 ppolymorphe 175
0 OriginalGriff 7,231
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,356
3 George Jonsson 2,865
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web02 | 2.8.151120.1 | Last Updated 31 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100