简体   繁体   中英

Android: API Compatibility issues in build.gradle

My application stops working without any log when I use the API 19 instead of the API 8

I'm using the code from this tutorial: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/

This is the gradle configuration that works perfectly:

apply plugin: 'android'

android {
    compileSdkVersion 8
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 8
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

I want to update it so it works with another code that I'm using and requires a recent API, so I modify the gradle configuration to:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

It suddenly stops working when openning a particular screen without any log message, this is the .java of that screen that fails when it gets openned (it receives data from a json call on a .php script and displays it on a webView):

package com.example.androidhive;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class JobDescriptionActivity extends Activity {

    TextView txtJobID;
    TextView txtCompanyName;
    TextView txtCompanyHeadline;
    TextView txtJobLocation;
    WebView webViewDesc;

    String job_id;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    // single product url
    private static final String url_product_detials = "http://10.0.2.2/android_connect/tuivel/get_job_details.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_JOB = "job";
    private static final String TAG_PID = "job_id";
    private static final String TAG_JOBLOCATION = "job_location";
    private static final String TAG_JOBID = "job_id";
    private static final String TAG_COMPANYNAME = "company_name";
    private static final String TAG_COMPANYHEADLINE = "company_headline";
    private static final String TAG_DESCRIPTION = "job_description";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.job_description);

        // getting product details from intent
        Intent i = getIntent();

        // getting product id (pid) from intent
        job_id = i.getStringExtra(TAG_PID);

        // Getting complete product details in background thread
        new GetProductDetails().execute();

        // send result code 100 to notify about product update
        setResult(100);
        //finish();


    }

    /**
     * Background Async Task to Get complete product details
     * */
    class GetProductDetails extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(JobDescriptionActivity.this);
            pDialog.setMessage("Loading job details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Getting product details in background thread
         * */
        protected String doInBackground(String... params) {

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    // Check for success tag
                    int success;
                    try {
                        // Building Parameters
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("job_id", job_id));

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

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

                        // json success tag
                        success = json.getInt(TAG_SUCCESS);
                        if (success == 1) {

                            // successfully received product details
                            JSONArray productObj = json.getJSONArray(TAG_JOB); // JSON Array

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

                            // product with this pid found
                            // Edit Text
                            //txtJobTitle = (TextView) findViewById(R.id.jobDescTitle);
                            txtJobID = (TextView) findViewById(R.id.jobID);

                            txtCompanyName = (TextView) findViewById(R.id.companyName);
                            txtCompanyHeadline = (TextView) findViewById(R.id.companyHeadline);
                            txtJobLocation = (TextView) findViewById(R.id.jobLocation);
                            webViewDesc = (WebView) findViewById(R.id.webView);

                            // display product data in EditText
                            txtJobID.setText(product.getString(TAG_JOBID));
                            txtCompanyName.setText(product.getString(TAG_COMPANYNAME));
                            txtCompanyHeadline.setText(product.getString(TAG_COMPANYHEADLINE));
                            txtJobLocation.setText(product.getString(TAG_JOBLOCATION));
                            webViewDesc.loadData("<html>"+product.getString(TAG_DESCRIPTION)+"</html>", "text/html",null);

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

            return null;
        }


        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
    }

}

The XML if needed:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F7F7F7"
    android:orientation="vertical" >

    <!-- Job id (pid) - will be HIDDEN - here if needed -->
    <TextView
        android:id="@+id/jobID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />


    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#F1F1F1"
        android:orientation="horizontal"
        android:padding="5dip" >

        <!--  ListRow Left sied Thumbnail image -->
        <LinearLayout android:id="@+id/thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dip"
            android:layout_alignParentLeft="true"
            android:background="@drawable/image_bg"
            android:layout_marginRight="5dip">

            <ImageView
                android:id="@+id/list_image"
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:src="@drawable/rihanna"/>

        </LinearLayout>

        <!-- Company Name-->
        <TextView
            android:id="@+id/companyName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/thumbnail"
            android:layout_toRightOf="@+id/thumbnail"
            android:layout_marginLeft="5dip"
            android:text="Company"
            android:textColor="#555555"
            android:typeface="sans"
            android:textSize="15dip"
            android:textStyle="bold"/>

        <!-- Company Headline -->
        <TextView
            android:id="@+id/companyHeadline"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/companyName"
            android:textColor="#666666"
            android:textSize="10dip"
            android:layout_marginTop="1dip"
            android:layout_marginLeft="5dip"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="Job Title" />

        <!-- Job Location -->
        <TextView
            android:id="@+id/jobLocation"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/companyHeadline"
            android:layout_marginLeft="5dip"
            android:textColor="#777777"
            android:textSize="10dip"
            android:layout_marginTop="1dip"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="Job Info" />

    </RelativeLayout>

    <WebView
        android:id="@+id/webView"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:background="#FFFFFF"
        android:layout_weight="0.9" />

</LinearLayout>

I would doubt of any tutorial that suggests using runOnUiThread() from an AsyncTasks 's doInBackground() or any tutorial that uses runOnUIThread() in AsyncTasks ' onPostExecute() (it already runs on the UI Thread ).

The Exception that you are getting (or will get for sure if is not this one) is a NetworkOnMainThread . If you want to update the UI after doInBackground() completes, you should do it in onPostExecute() .

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