简体   繁体   中英

remote server connection in android application

I have created class that retrieves data from a MySQL database and displays it into various edit texts as shown below. When testing the application on my Local host it is working and retrieving the data from a JSON array through a PHP file, however when I change the URL for the HTTPRequest from Local host http://10.0.2.2/concurency/get_user_details_test.php to the remote host, http://concurrenceypule.netau.net/get_user_details_test.php the application running on V 2.3, is crushing. All the information regarding the remote database connection is working as it is displaying the data through the web browser properly. What could be causing the application to be crushing, and how can it be resolved.

    private static final String url_product_detials = "http://concurrenceypule.netau.net/get_user_details_test.php";

class GetProductDetails extends AsyncTask<String, String, String> {
    protected String doInBackground(String... params) {
     final String user_name2 = "paul";
        runOnUiThread(new Runnable() {
            public void run() {
                int success;
                try {
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("user_name",user_name2));
                    JSONObject json = jsonParser.makeHttpRequest(
                            url_product_detials, "GET", params);
                    Log.d("Single Product Details", json.toString());
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        JSONArray productObj = json
                                .getJSONArray(TAG_PRODUCT); 
                            JSONObject product = productObj.getJSONObject(0);

                        txtName = (EditText) findViewById(R.id.username);
                        txtcontact = (EditText) findViewById(R.id.tv_contact);
                        txtemail = (EditText) findViewById(R.id.tv_email);
                        txtaddress = (EditText) findViewById(R.id.et_Address);

                        txtName.setText(product.getString(TAG_NAME));
                        txtcontact.setText(product.getString(TAG_contact));
                        txtemail.setText(product.getString(TAG_email));
                        txtaddress.setText(product.getString(TAG_address));
                    }else{

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        return null;
    }
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
    }
}

Does your TAG contains a multiple ? record or singular query record is fetch every time if its singular put your following section of code in OnPostExecute(),remove runnable in doInBackground() beacause your code is already in thread..

        txtName = (EditText) findViewById(R.id.username);
                        txtcontact = (EditText) findViewById(R.id.tv_contact);
                        txtemail = (EditText) findViewById(R.id.tv_email);
                        txtaddress = (EditText) findViewById(R.id.et_Address);

                        txtName.setText(product.getString(TAG_NAME));
                        txtcontact.setText(product.getString(TAG_contact));
                        txtemail.setText(product.getString(TAG_email));
                        txtaddress.setText(product.getString(TAG_address));

Store your record in variable in doInBackground() and then pass to edittext object

String temp=product.getString(TAG_NAME);
txtName.setText(temp);

in OnPostMethod() add

super.onPostExecute(file_url); 

Add these lines in Manifest.XML file if you didn't put on it.

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

finally check your LogCat is your JSON is parsing properly and then update your question or answer and don't jump for liking and disliking first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM