1

我正在尝试通过我的 android 应用程序在我的网络服务上传递我的位置。但我现在有这样的错误。

日志猫:

02-24 21:37:35.438: E/AndroidRuntime(657): FATAL EXCEPTION: main
02-24 21:37:35.438: E/AndroidRuntime(657): android.os.NetworkOnMainThreadException
02-24 21:37:35.438: E/AndroidRuntime(657):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
02-24 21:37:35.438: E/AndroidRuntime(657):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
02-24 21:37:35.438: E/AndroidRuntime(657):  at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
02-24 21:37:35.438: E/AndroidRuntime(657):  at libcore.io.IoBridge.connect(IoBridge.java:112)
02-24 21:37:35.438: E/AndroidRuntime(657):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
02-24 21:37:35.438: E/AndroidRuntime(657):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
02-24 21:37:35.438: E/AndroidRuntime(657):  at java.net.Socket.connect(Socket.java:842)
02-24 21:37:35.438: E/AndroidRuntime(657):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
02-24 21:37:35.438: E/AndroidRuntime(657):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
02-24 21:37:35.438: E/AndroidRuntime(657):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-24 21:37:35.438: E/AndroidRuntime(657):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-24 21:37:35.438: E/AndroidRuntime(657):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-24 21:37:35.438: E/AndroidRuntime(657):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-24 21:37:35.438: E/AndroidRuntime(657):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-24 21:37:35.438: E/AndroidRuntime(657):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-24 21:37:35.438: E/AndroidRuntime(657):  at com.example.projectthesis.JSONParser.makeHttpRequest(JSONParser.java:51)
02-24 21:37:35.438: E/AndroidRuntime(657):  at com.example.projectthesis.LocationGetter.onLocationChanged(LocationGetter.java:138)
02-24 21:37:35.438: E/AndroidRuntime(657):  at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:234)
02-24 21:37:35.438: E/AndroidRuntime(657):  at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:167)
02-24 21:37:35.438: E/AndroidRuntime(657):  at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:183)
02-24 21:37:35.438: E/AndroidRuntime(657):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 21:37:35.438: E/AndroidRuntime(657):  at android.os.Looper.loop(Looper.java:137)
02-24 21:37:35.438: E/AndroidRuntime(657):  at android.app.ActivityThread.main(ActivityThread.java:4424)
02-24 21:37:35.438: E/AndroidRuntime(657):  at java.lang.reflect.Method.invokeNative(Native Method)
02-24 21:37:35.438: E/AndroidRuntime(657):  at java.lang.reflect.Method.invoke(Method.java:511)
02-24 21:37:35.438: E/AndroidRuntime(657):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-24 21:37:35.438: E/AndroidRuntime(657):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-24 21:37:35.438: E/AndroidRuntime(657):  at dalvik.system.NativeStart.main(Native Method)

**

我已经把我的网络传递代码放到 onLocationChange 部分,这里是:

位置获取器:

public class LocationGetter extends MapActivity implements LocationListener { // <1>
    private static final String TAG = "LocationActivity";
        LocationManager locationManager; // <2>
    Geocoder geocoder; // <3>
    TextView locationText;
    MapView map;
    MapController mapController; // <4>

// Progress Dialog
private ProgressDialog pDialog;
//JSONParser Object creation
JSONParser jsonParser = new JSONParser();
// url to pass location to web
//private static String url_create_product = "http://student-thesis.netii.net/location_adding.php";
private static String url_create_product = "http://10.0.2.2/TheCalling/location_adding.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps);
    locationText = (TextView) this.findViewById(R.id.lblLocationInfo);
    map = (MapView) this.findViewById(R.id.mapview);
    map.setBuiltInZoomControls(true);
    mapController = map.getController(); // <4>
    mapController.setZoom(19);
    locationManager = (LocationManager) this
            .getSystemService(LOCATION_SERVICE); // <2>
    geocoder = new Geocoder(this); // <3>
    Location location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); // <5>
    if (location != null) {
        Log.d(TAG, location.toString());
        this.onLocationChanged(location); // <6>
    }
}
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            60000, 5, this); // <7>
}
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this); // <8>
}
@Override
public void onLocationChanged(Location location) { // <9>
    Log.d(TAG, "onLocationChanged with location " + location.toString());
    String text = String.format(
            "Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f \n",
            location.getLatitude(), location.getLongitude(),
            location.getAltitude(), location.getBearing());
    this.locationText.setText(text);
    try {
        List<Address> addresses = geocoder.getFromLocation(
                location.getLatitude(), location.getLongitude(), 10); // <10>
        for (Address address : addresses) {
            this.locationText.append(" " + address.getAddressLine(0));
        }
        int latitude = (int) (location.getLatitude() * 1000000);
        int longitude = (int) (location.getLongitude() * 1000000);      

        GeoPoint point = new GeoPoint(latitude, longitude);
        mapController.animateTo(point); // <11>

        List<Overlay> mapOverlays = map.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.reddot);
        AddItemizedOverlay itemizedOverlay =
        new AddItemizedOverlay(drawable, this);

        OverlayItem overlayitem = new OverlayItem(point, "Hello", "Sample Overlay item");

        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);

        String strLatitude = Integer.toString(latitude);
        String strLongitude = Integer.toString(longitude);

        //Building parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("latitude", strLatitude));
        params.add(new BasicNameValuePair("longitude", strLongitude));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                // successfully updated
                Toast.makeText(getBaseContext(), "Location Passing successful", Toast.LENGTH_SHORT).show();
            } else {
                // failed to create product
                Toast.makeText(getBaseContext(), "Location Passing Unsuccessful", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


    } catch (IOException e) {
        Log.e("LocateMe", "Could not get Geocoder data", e);
    }
}
@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}

我该如何处理?

4

4 回答 4

1

在您的 onLocationChanged 中启动一个自定义 AsyncTask 并在他的 doInBackground 方法中放入所有使用 http 连接(如 jsonParser.makeHttpRequest)的代码。

然后在 AsyncTask 的 postExecute 方法中,您应该更新数据和/或 ui。

于 2013-02-24T03:28:35.790 回答
1

我的天啊。不允许在主线程上进行网络操作。当然,除非您希望您的用户讨厌您。

AsyncTask 工作正常,但我认为使用后台服务 (IntentService) 可以获得更好的结果。这将您的位置上传与您的前景分离,这将允许您缓存更新等。

另外,我注意到您在尝试发送数据之前没有检查您是否有网络连接。似乎tubez无处不在:)。

于 2013-02-24T03:31:50.870 回答
0

在这里看到这个问题。您不能在主线程上进行网络操作。无论是 Mark sed 还是 AsyncTask

或(这是不推荐的)

您可以在主线程中允许网络操作。这不好,因为它会锁定你的 UI,给用户带来不好的体验。

StrictMode.ThreadPolicy policy = new StrictMode. ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy);
于 2013-02-24T03:24:29.323 回答
0

我的网络服务也有这样的例外。发生这种情况是因为我没有在异步任务私有类中进行 Web 服务调用。您应该尝试将代码放在该类中,这样它就不会导致网络超时。

如果这没有帮助,我将删除我的答案。我不想有一个糟糕的答案作为唯一的答案。只是评论

于 2013-02-24T03:18:27.720 回答