Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6102405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:36:36+00:00 2026-05-23T13:36:36+00:00

I am trying to read a database in my from a remote DB, for

  • 0

I am trying to read a database in my from a remote DB, for that i am using a php code to connect to my Mysql DB and to query my DB:

Name of the file: check.php:

<?php
    require_once("php/dbconnect.php");
    $query = "SELECT name FROM user ";
    $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());

    while ( $row = mysql_fetch_array( $result ) ) {
        $output[]=$row['name'];
    }

    print(json_encode($output));
    mysql_close();
 ?/>

And the Java (Android) code to connect to the remote DB:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
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.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.net.URI;

public class ConnectToMyDb extends Activity {
    InputStream is;
    private HttpPost httppost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String result = "";

        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://www.Myurl.com/check.php");

            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("log_tag", "connection success ");
            Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
            Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();

        }

        //convert response to string
        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 + "\n");
                    Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
        }

        is.close();

        result=sb.toString();
    }catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());
        Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
    }

    //parse json data
    try{
        //ArrayList<Messages> result1 = new ArrayList<Messages>();
        JSONArray jArray = new JSONArray(result);
        JSONObject json_data= new JSONObject(result);

        for(int i=0;i<jArray.length();i++){
            json_data = jArray.getJSONObject(i);

            Log.i("log_tag"," name: "+json_data.getString("name");
                    Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
        }
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
        Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
    }
}

My problem its not the connection but the parsing of the json i get an exception:

Error parsing data org.json.JSONException:  
Value <!DOCTYPE of type java.lang.String cannot  be converted to JSONArray
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:36:37+00:00Added an answer on May 23, 2026 at 1:36 pm

    I would get rid of this line:

    JSONArray jArray = new JSONArray(result);
    

    and do this instead:

    JSONObject json_data = new JSONObject(result);
    JSONArray nameArray = json_data.names();
    JSONArray valArray = json.toJSONArray(nameArray);
    

    I believe this is correct, nonetheless, please study this tutorial, it will work for you:

    Android as a Restful Client

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read data from SQL Server database using Perl and the DBI
I'm trying to read a .doc file into a database so that I can
While trying to connect to a database that's set to read only, the connection
I am trying to read database scripts in a Jar file so that I
I have an id that i am trying to read from my database and
I am trying to connect to a local MySQL database from a VB6 application.
I am trying to read binary from database and write as a file in
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy
im having some problems trying to read a SQlite data base from flash using
Trying to read an RSS and select information using Linq but can't seem to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.