1

我确实在一篇文章中了解到这一点,在 3.0 及更高版本上,必须使用AsyncTask来执行任何网络活动。知道了这一点,我使用AsyncTask来调用JSON请求,我的主类如下所示:

主页.java

public class doTheFare extends AsyncTask<Void,Void,Void>
{

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        progress = new ProgressDialog(MainPage.this);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progress.setMessage("Processing Request ...");
        progress.show();
        UserFunctions users = new UserFunctions();
        jObj = users.distanceGET(location.getText().toString(), destination.getText().toString());
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        try{
            jArr = jObj.getJSONArray("rows");
            jObj1 = jArr.getJSONObject(0);
            jArr2 = jObj1.getJSONArray("elements");
            jObj2 = jArr2.getJSONObject(0);
            jObj3 = jObj2.getJSONObject("distance");
            distance_value = jObj3.getString("text");
            String value = jObj3.getString("value");
            SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            String sF = getPrefs.getString("startFare", "10");
            String pM = getPrefs.getString("perKilo", "27");
            startFare = Double.parseDouble(sF);
            perKilo = Double.parseDouble(pM);
            perMeter=perKilo/5;
            int parseDistance = Integer.parseInt(value);
            double fare = startFare+((parseDistance/200)*perMeter);
            int roundedValue = (int)Math.round(fare);
            //Fare to send
            taxiFare = String.valueOf(roundedValue);
            Log.i("Fare Estimate", taxiFare);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        progress.dismiss();
        from_split = location.getText().toString();
        to_split = destination.getText().toString();
        if(from_split.contains(","))
        {
            from_fill = from_split.split(",");
        }
        else
        {
            from_fill = from_split.split(" ");
        }
        if(to_split.contains(","))
        {
            to_fill = to_split.split(",");
        }
        else
        {
            to_fill = to_split.split(" ");
        }

        send_location=from_fill[0];
        send_destination=to_fill[0];
        send_fare = taxiFare;
        send_distance = distance_value;
        if(isMode.equalsIgnoreCase(""))
        {
            Calendar c = Calendar.getInstance();
            int hours = c.get(Calendar.HOUR_OF_DAY);
            if(hours<6)
                isMode = "nMode";
            else if(hours>=21)
                isMode = "nMode";
            else
                isMode = "dMode";
        }
        Intent toResults = new Intent(MainPage.this, ResultsDisplay.class);
        Bundle extras = new Bundle();
        extras.putString("TO", send_location);
        extras.putString("FROM", send_destination);
        extras.putString("FARE", send_fare);
        extras.putString("DISTANCE", send_distance);
        extras.putString("GMAPFROM", from_split.toString());
        extras.putString("GMAPTO", to_split.toString());
        extras.putString("MODE",isMode);
        toResults.putExtras(extras);
        startActivity(toResults);
        super.onPostExecute(result);
    }

}

在这个Activity我有一个OnClickListener调用它,AsyncTask即使程序给了我以下

05-10 03:02:19.823: E/AndroidRuntime(1815): FATAL EXCEPTION: main
05-10 03:02:19.823: E/AndroidRuntime(1815): android.os.NetworkOnMainThreadException
05-10 03:02:19.823: E/AndroidRuntime(1815):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at com.example.calculator_taxi_fare.functions.JSONParser.getJSONFromUrl(JSONParser.java:56)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at com.example.calculator_taxi_fare.functions.UserFunctions.distanceGET(UserFunctions.java:28)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at com.example.calculator_taxi_fare.MainPage$doTheFare.onPreExecute(MainPage.java:251)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at android.os.AsyncTask.execute(AsyncTask.java:534)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at com.example.calculator_taxi_fare.MainPage$6.onClick(MainPage.java:230)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at android.view.View.performClick(View.java:4202)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at android.view.View$PerformClick.run(View.java:17340)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at android.os.Handler.handleCallback(Handler.java:725)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at android.os.Looper.loop(Looper.java:137)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at java.lang.reflect.Method.invoke(Method.java:511)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-10 03:02:19.823: E/AndroidRuntime(1815):     at dalvik.system.NativeStart.main(Native Method)
    location.setOnItemClickListener(new OnItemClickListener()
    {

我是否以正确的方式实施?请帮忙!

我还在这个应用程序中实现了谷歌,其中结果/建议确实显示但在出现错误时PlacesAutocomplete无法选择。代码如下:3.0 or higherUnexpected value from nativeGetEnabledTags: 0

location.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        // TODO Auto-generated method stub
        autoLocation = (Places) adapterView.getItemAtPosition(position);
        location.setText(autoLocation.description);
        location.clearFocus();
        destination.requestFocus();
        Log.d("AutoCompleteTask", location.getText().toString());
            }
});
destination.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
destination.setOnItemClickListener(new OnItemClickListener() 
{

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            // TODO Auto-generated method stub
        autoDestination = (Places) adapterView.getItemAtPosition(position);
        destination.setText(autoDestination.description);
        destination.clearFocus();
        calculate.requestFocus();
        Log.d("AutoCompleteTask2", destination.getText().toString());
            }
});
4

4 回答 4

3

似乎这段代码正在从网络获取数据。你已经把它放在工作线程中,把它放在 doinBackground();

        jObj = users.distanceGET(location.getText().toString(), destination.getText().toString());
于 2013-05-10T03:30:54.900 回答
1

看来你的distanceGET方法是做网络操作

根据这里的文档

onPreExecute(),在任务执行之前在 UI 线程上调用。此步骤通常用于设置任务,例如通过在用户界面中显示进度条。

做你的网络事情doInBackground

于 2013-05-10T03:33:32.050 回答
1

您必须在 doInBackground() 方法上设置您的网络操作

jObj = users.distanceGET(location.getText().toString(), destination.getText().toString())

必须写在 doInBackground()

于 2013-05-10T03:38:04.123 回答
1

不要在执行前进行网络操作。在doInBackground中做网络操作,才是正确的做法。

protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
UserFunctions users = new UserFunctions();
    jObj = users.distanceGET(location.getText().toString(),
           destination.getText().toString());
    try{
        jArr = jObj.getJSONArray("rows");
        jObj1 = jArr.getJSONObject(0);
        --------------------
        --------------------
        }catch(Exception e){
        }
}
于 2013-05-10T04:14:17.883 回答