-1

I am trying to retrieve data from MySQL database (which I access via phpMyAdmin) and trying to display the data in the EditText. I don't know where I am making a mistake, but want to get rid of the problem.

The error in the log cat shows that no value for cities. I know I am making mistake but I am new to Android. Below is my code:

MainActivity.java

public class MainActivity extends Activity {

    private ProgressDialog pDialog;
    String id;
    Button btn;
    EditText txt1,txt2;
    JSONParser jsonParser = new JSONParser();
    ArrayList<HashMap<String,String>> result; 
    private static final String url = "http://10.0.2.2/letstry/value.php";
    private static final String tag_success = "success";
    private static final String tag_cities = "cities";
    private static final String tag_id = "id";
    private static final String tag_city = "city_name";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        btn=(Button)findViewById(R.id.select);

        result = new ArrayList<HashMap<String,String>>();

        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                new loadresult().execute();
            }
        });
    }

    class loadresult extends AsyncTask<String,String,String>
    {

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
             int success;
             try {

                 // Building Parameters
                 List<NameValuePair> params = new ArrayList<NameValuePair>();
                 params.add(new BasicNameValuePair("id", id));

                 // getting product details by making HTTP request
                 // Note that product details url will use GET request
                 JSONObject json = jsonParser.makeHttpRequest(
                         url, "GET", params);

                 // check your log for json response
                 Log.d("Single Product Details", json.toString());

                 // json success tag
                 success = json.getInt(tag_success);
                 if (success == 1) {
                     // successfully received product details
                     JSONArray listObj = json
                             .getJSONArray(tag_cities); // JSON Array

                     // get first product object from JSON Array
                     JSONObject list = listObj.getJSONObject(0);

                     // product with this pid found
                     // Edit Text
                     txt1 = (EditText) findViewById(R.id.one1);
                    txt2 = (EditText) findViewById(R.id.two1);

                     // display product data in EditText
                     txt1.setText(list.getString(tag_id));
                     txt2.setText(list.getString(tag_city));

                 }else{
                     // product with pid not found
                 }
             } catch (JSONException e) {
                 e.printStackTrace();
             }
            return null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

PHP Script

<?php

$con = mysql_connect("localhost","root","12345");
$db = mysql_select_db("search",$con);

    $query = mysql_query("Select * from cities where Id=2");
if(mysql_num_rows($query)>0)
{   
    while($result=mysql_fetch_array($query))
    {
        $list=array();
        $list['Id']=$result['Id'];
        $list['city_name']=$result['city_name'];

        $response['list']=array();

        array_push($response["list"], $list);
    }
    $response["success"] = 1;
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);
}

?>
hcarrasko
  • 2,320
  • 6
  • 33
  • 45
Mikka
  • 55
  • 1
  • 7
  • u mean error of Asynctask in log cat? – Mikka Dec 09 '15 at 16:28
  • no, I think @kishore jethava wanted to tell you that you should read the documentation about AsyncTask/ Threads. There you will find that it you are attempting to do something which is forbidden, almost as forbidden as dividing by zero. Or even more forbidden ;-) **TL;DR**: [link](http://developer.android.com/guide/components/processes-and-threads.html) to documentation, especially part "Worker threads" – Bö macht Blau Dec 09 '15 at 16:38
  • you need to use a `Thread Handler`, because in Android you can't change view elements (in the main thread) from methods running in another threads. – hcarrasko Dec 09 '15 at 16:49
  • ill read it, as it is essential, but i need the solution too, urgently! – Mikka Dec 09 '15 at 16:52
  • @RAP: the edit reason is not for adding in comments from the OP - it is to explain to readers what changed between one version and another. – halfer Dec 09 '15 at 18:34
  • 1
    @Mikka: you'd be well advised not to request urgency here - often you'll just get extra downvotes in reply. Your readership is primarily volunteers, who will answer questions they find interesting, and at their leisure too. No question is more important than another on _Stack Overflow_. – halfer Dec 09 '15 at 18:35

1 Answers1

0

You need to use a Thread Handler, because in Android you can't change view elements (running in the main thread) from methods running in another threads.

Here is a nice axample from documentation. Another nice example has been already answered here

Hope it helps.

Community
  • 1
  • 1
hcarrasko
  • 2,320
  • 6
  • 33
  • 45