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

  • Home
  • SEARCH
  • 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 7663107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:58:24+00:00 2026-05-31T13:58:24+00:00

Iam trying to make a connection between my database on the MYSQL(iam using the

  • 0

Iam trying to make a connection between my database on the MYSQL(iam using the WAMP server) and android application , but when i run the application an exception is raised , i fixed the log cat and its points to this statement
jArray = new JSONArray(result);

03-18 12:48:59.580: E/AndroidRuntime(458):  at org.json.JSONArray.<init>(JSONArray.java:87)

why this exception occur ?and how i can solve it ?

thanks in advanced..

this is my code :
City.php:

          <?php
           mysql_connect("localhost","username","password");
           mysql_select_db("Deal");
           $sql=mysql_query("select * from City where Name like 'R%' ");
           while($row=mysql_fetch_assoc($sql))
           $output[]=$row;
           print(json_encode($output));
           mysql_close();
              ?>

the java class:

public class ConnectionActivity extends ListActivity {

int ct_id;
String[] ct_name = null;
JSONArray jArray;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;

    // http post
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://localhost/city.php");
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection" + e.toString());
            }

            // convert response to string
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                sb = new StringBuilder();
                sb.append(reader.readLine() + "\n");
                String line = "0";
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }

            // paring data

            try {
                jArray = new JSONArray(result);
                JSONObject json_data = null;
                ct_name = new String[jArray.length()];
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    ct_id = json_data.getInt("City_ID");
                    ct_name[i] = json_data.getString("Name");
                }
            } catch (JSONException e1) {
                Toast.makeText(getBaseContext(), "No City Found", Toast.LENGTH_LONG)
                        .show();
            } catch (ParseException e1) {
                e1.printStackTrace();
            }

            ListView lv;
            lv = getListView();
            lv.setTextFilterEnabled(true);
            ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, ct_name);
            setListAdapter(adapter);



}
public void onListItemClick(ListView parent, View v, int position, long id)
{
        Toast.makeText(this,"You have selected " + ct_name[position],Toast.LENGTH_SHORT).show();
        }
}                      

log:

             03-18 13:28:46.923: W/ActivityThread(623): Application Com.Connection is waiting for the debugger on port 8100...
             03-18 13:28:46.973: I/System.out(623): Sending WAIT chunk
             03-18 13:28:46.983: I/dalvikvm(623): Debugger is active
             03-18 13:28:47.174: I/System.out(623): Debugger has connected
             03-18 13:28:47.183: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:47.383: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:47.583: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:47.783: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:47.993: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:48.193: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:48.403: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:48.603: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:48.803: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:49.003: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:49.203: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:49.413: I/System.out(623): waiting for debugger to settle...
             03-18 13:28:49.613: I/System.out(623): debugger has settled (1497)
             03-18 13:30:17.593: E/log_tag(623): Error in http       connectionandroid.os.NetworkOnMainThreadException
               03-18 13:30:34.315: E/log_tag(623): Error converting result java.lang.NullPointerException
  • 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-31T13:58:27+00:00Added an answer on May 31, 2026 at 1:58 pm

    You get an error as you try to perform HTTP connection on the main thread:

    03-18 13:30:17.593: E/log_tag(623): Error in http       connectionandroid.os.NetworkOnMainThreadException
    

    As far as I can see, the HTTP code is correct, but you are running it on the UI thread. This is not allowed. You need to perform such long actions on a background thread. Android gives you a very easy to use facilities to perform long action on the background and publish the results on the main (UI) thread. One of the most useful is AsyncTask. I would recommend that you read Painless Threading, as it will help you a lot IMO.

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

Sidebar

Related Questions

Hi iam trying to make an update trigger in my database. But i get
I am trying to make a simple Client-Server application but when I execute the
I am trying to make connections to my database using connection pooling library: DBPool
So currently my company is using a TCP/IP connection to talk between server and
I am structuring a modular php application using MySQL as my database. I would
I am trying to make an Android/ Java application that needs to connect to
I am trying to make a secure connection to a OCS server through https
I have made a database structure where users can make friend connection between them
I'm trying to make a communication between a PHP page and running Java server.
Hi iam trying to make an Wpf TextBlock to blink. I want like when

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.