android中的http连接错误

时间:2012-06-07 11:23:38

标签: android json http

我收到此错误。我不知道出了什么问题。有人可以帮我吗?搜索了一下,但不知道如何解决它...这有点紧急。

这是我的代码:

   package nearest.moskee.almawada;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.google.android.maps.GeoPoint;

public class MoskeeJSON extends Activity {
    private Handler mHandler = new Handler(Looper.getMainLooper());
    String url;
    HttpClient httpclient;
    InputStream is = null;

    private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

        ProgressDialog mProgressDialog;

        @Override
        protected void onPostExecute(Void result) {
            mProgressDialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            mProgressDialog = ProgressDialog.show(MoskeeJSON.this,
                    "Loading...", "Data is Loading...");

        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                HttpPost httppost = new HttpPost(url);
                //HttpGet httppost = new HttpGet(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Log.e("url", url);
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
            }

            return null;
        }

    }

    public JSONArray getCloseMosquesInJSON(float lat, float lng, float radius) {

        String result = "";

        try {
            httpclient = new DefaultHttpClient();
            url = "http://www.moskee.org/finder/getCloseDataInJSON.php?lat="
                    + lat + "&lng=" + lng + "&radius=" + radius;
            new MyAsyncTask().execute();
            Log.v("URL =>", url);

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            Log.e("result", result);
            Log.e("resultLength", result.length() + "");
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }
        // parse json data
        if (result.length() == 5) {
            Log.i("result is ", "LEEG");
            return null;
        } else {
            JSONArray jArray = null;
            try {
                jArray = new JSONArray(result);
            } catch (JSONException e) {
                Log.e("log_tag", "Error parrsing data " + e.toString());
            }

            return jArray;
        }
    }

    JSONObject jsonObject;
    StringBuilder stringBuilder;

}

错误

  

06-07 15:04:07.329:I / dalvikvm(1643):threadid = 3:对信号3作出反应   06-07 15:04:07.370:I / dalvikvm(1643):写了堆栈跟踪   '/data/anr/traces.txt'06-07 15:04:07.828:I / dalvikvm(1643):   threadid = 3:对信号作出反应3 06-07 15:04:07.898:I / dalvikvm(1643):   把堆栈痕迹写成'/data/anr/traces.txt'06-07 15:04:07.988:   D / gralloc_goldfish(1643):未检测到GPU仿真的仿真器。   06-07 15:04:24.098:D / InputEventConsistencyVerifier(1643):KeyEvent:   ACTION_UP但关键是没有关闭。 06-07 15:04:24.098:   D / InputEventConsistencyVerifier(1643):in   android.widget.EditText@412aa7f0 06-07 15:04:24.098:   D / InputEventConsistencyVerifier(1643):0:发送于16907476000000,   KeyEvent {action = ACTION_UP,keyCode = KEYCODE_TAB,scanCode = 15,   metaState = 0,flags = 0x8,repeatCount = 0,eventTime = 16907476,   downTime = 16907424,deviceId = 0,source = 0x301} 06-07 15:04:24.098:   D / InputEventConsistencyVerifier(1643): - 最近的事件 - 06-07   15:04:24.098:D / InputEventConsistencyVerifier(1643):1:发送于   16902546208000,MotionEvent {action = ACTION_UP,id [0] = 0,x [0] = 229.0,   y [0] = 2.0,toolType [0] = TOOL_TYPE_FINGER,buttonState = 0,metaState = 0,   flags = 0x0,edgeFlags = 0x0,pointerCount = 1,historySize = 0,   eventTime = 16902546,downTime = 16902470,deviceId = 0,source = 0x1002}   06-07 15:04:24.098:D / InputEventConsistencyVerifier(1643):2:已发送   在16902470916000,MotionEvent {action = ACTION_DOWN,id [0] = 0,   x [0] = 229.0,y [0] = 2.0,toolType [0] = TOOL_TYPE_FINGER,buttonState = 0,   metaState = 0,flags = 0x0,edgeFlags = 0x0,pointerCount = 1,historySize = 0,   eventTime = 16902470,downTime = 16902470,deviceId = 0,source = 0x1002}   06-07 15:04:27.649:V / Adres =&gt;(1643):Pierenbergstraat,Antwerpen   06-07 15:04:27.649:V / IL GeoPoint纬度=&gt;(1643):51.18809127807617   06-07 15:04:27.669:V / IL GeoPoint经度=&gt;(1643):   4.380319118499756 06-07 15:04:27.679:E / GA(1643):GA 06-07 15:04:28.219:I / dalvikvm(1643):threadid = 3:对信号3 06-07作出反应   15:04:28.339:I / dalvikvm(1643):写了堆栈跟踪   '/data/anr/traces.txt'06-07 15:04:28.419:D / dalvikvm(1643):   GC_CONCURRENT释放192K,3%免费9323K / 9607K,暂停7ms + 6ms 06-07   15:04:28.419:W / CursorWrapperInner(1643):光标没有完成   previous close()06-07 15:04:28.419:W / CursorWrapperInner(1643):光标   没有事先关闭的最终确定()06-07 15:04:28.719:I / dalvikvm(1643):   threadid = 3:对信号作出反应3 06-07 15:04:28.719:D / dalvikvm(1643):   GC_FOR_ALLOC释放54K,3%免费9653K / 9927K,暂停54ms 06-07   15:04:28.829:I / dalvikvm(1643):写了堆栈跟踪   '/data/anr/traces.txt'06-07 15:04:28.909:E / log_tag(1643):错误   http连接java.lang.NullPointerException 06-07 15:04:28.909:   E / log_tag(1643):转换结果时出错   java.lang.NullPointerException 06-07 15:04:28.909:E / log_tag(1643):   错误标记数据org.json.JSONException:字符输入结束   0 of 06-07 15:04:29.110:I / Center map?(1643):51188091,4380319 06-07   15:04:29.110:我/中心地图?(1643):Toegepast zonder erreurs 06-07   15:04:29.259:I / dalvikvm(1643):threadid = 3:对信号3 06-07作出反应   15:04:29.370:I / dalvikvm(1643):写了堆栈跟踪   '/data/anr/traces.txt'06-07 15:04:29.649:I / MapActivity(1643):   处理网络变更通知:CONNECTED 06-07 15:04:29.649:   E / MapActivity(1643):无法获得连接工厂客户端06-07   15:04:29.810:I / dalvikvm(1643):threadid = 3:对信号3 06-07作出反应   15:04:29.910:I / dalvikvm(1643):写了堆栈跟踪   '/data/anr/traces.txt'06-07 15:04:30.089:D / dalvikvm(1643):   GC_FOR_ALLOC释放32K,3%免费9946K / 10183K,暂停59ms 06-07   15:04:30.229:I / dalvikvm(1643):threadid = 3:对信号3 06-07作出反应   15:04:30.389:I / dalvikvm(1643):写了堆栈跟踪   '/data/anr/traces.txt'06-07 15:04:31.499:D / dalvikvm(1643):   GC_CONCURRENT释放99K,2%免费10360K / 10567K,暂停8ms + 7ms

MoskeeResult

     package nearest.moskee.almawada;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MoskeeResults extends MapActivity {
    MapView mapView;
    MapController mc;
    GeoPoint p;

    @SuppressWarnings("unused")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.myMap);
        mapView.setBuiltInZoomControls(true);

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(
                R.drawable.iconmoskee);
        OverlayItems itemizedoverlay = new OverlayItems(drawable,this);

        try {
            MoskeeJSON m = new MoskeeJSON();

            Bundle extras = getIntent().getExtras();
            float lat = 0;
            float lng = 0;
            float radius = 1;
            lat = (float) extras.getDouble("lat");
            lng = (float) extras.getDouble("lng");


            JSONArray arrMosques = m.getCloseMosquesInJSON(lat, lng, radius);


            if (arrMosques != null) {
                Log.i("Array aangemaakt", "Array is aangemaakt, het bevat "
                        + arrMosques.length() + " moskeëen");
                for (int i = 0; i < arrMosques.length(); i++) {
                    JSONObject row;
                    row = arrMosques.getJSONObject(i);
                    double jlat = row.getDouble("Latitude");
                    double jlng = row.getDouble("Longitude");
                    OverlayItem overlayitem = new OverlayItem(new GeoPoint(
                            (int) (jlat * 1e6), (int) (jlng * 1e6)),
                            row.getString("Naam"), row.getString("Straat"));
                    itemizedoverlay.addOverlay(overlayitem);
                    Log.i("=>", "Another mosque is added to the map");

                }
                Log.i("Array aangemaakt", "Array is aangemaakt, het bevat "
                        + arrMosques.length() + " moskeëen");
                AlertDialog alert = new AlertDialog.Builder(this)
                        .setTitle("Aantal gevonden moskeëen")
                        .setMessage(
                                "Er zijn " + arrMosques.length()
                                        + " moskeëen gevonden in deze straal")
                        .create();
                alert.show();
            } else {
                AlertDialog alert = new AlertDialog.Builder(this)
                        .setTitle("Aantal gevonden moskeëen")
                        .setMessage(
                                "Er zijn geen moskeëen gevonden in deze straal.\nProbeer opnieuw te zoeken met een groter bereik")
                        .create();
                alert.show();
            }


            mapOverlays.add(itemizedoverlay);

            mc = mapView.getController();

            GeoPoint center = new GeoPoint((int) (lat * 1e6), (int) (lng * 1e6));
            Log.i("Center map?", "" + (int) (lat * 1e6) + ","
                    + (int) (lng * 1e6));
            mc.setCenter(center);
            Log.i("Center map?", "Toegepast zonder erreurs");
            mc.setZoom(17);

        } catch ( JSONException e) {
            // TODO Auto-generated catch block
            Log.i("JASOEN EXCEPTIOEN",
                    "I have absolutely no idea what's going on..."
                            + e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

1 个答案:

答案 0 :(得分:0)

正如您所指出的,错误位于您记录的行中。

JSONArray arrMosques = m.getCloseMosquesInJSON(lat, lng, radius);
Log.i("Array aangemaakt", "Array is aangemaakt, het bevat " + arrMosques.length() + " moskeëen");

在您之前调用m.getCloseMosquesInJson()之前,此方法可能会在以下代码块中返回null:

if (result.length() == 5) {
    Log.i("result is ", "LEEG");
    return null; // HERE
} else {
    JSONArray jArray = null;
    try {
        jArray = new JSONArray(result);
    } catch (JSONException e) {
        Log.e("log_tag", "Error parrsing data " + e.toString());
    }
    return jArray; // HERE! might be null when JSONException was thrown
}

在记录数组长度之前,您应该检查null