2012-06-07 72 views
-1

我得到這個錯誤。我不知道什麼是錯的。有人可以幫我嗎?搜索了一下,但不知道如何解決它......這是有點緊急。在android中的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):主題ID = 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):在 [email protected] 06-07 15:04:24.098: d/InputEventConsistencyVerifier(1643):0:169074.76億送出, 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,ed 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 =>(1643):Pierenbergstraat ,安特衛普 06-07 15:04:27.649:V/IL GeoPoint緯度=>(1643):51.18809127807617 06-07 15:04:27.669:V/IL GeoPoint經度=>(1643): 4.380319118499756 06-07 15:04:27.679:E/GA(16 43):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):光標在沒有 之前關閉()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):Error in http connection 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:I/Center map?(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的+ 7毫秒

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; 
    } 

} 
+0

你的MoskeeResults.java中的第87行是什麼? 86-87 – WarrenFaith

+0

行:Log.i( 「陣列aangemaakt」, 「陣列是aangemaakt,HET bevat」 \t \t \t \t \t + arrMosques.length()+ 「moskeëen」); – mXX

+0

此代碼不包含在您粘貼在此處的代碼中。無論如何,變量'arrMosques'爲空。 – WarrenFaith

回答

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

+0

Oke ill現在檢查它並嘗試解決它。我讓你知道它是怎麼回事! – mXX

+0

林已經檢查arrMosques – mXX

+0

只是它下面,我有: 如果 // ... 其他 ... 我沒有像你說的(arrMosques!= NULL):香港專業教育學院登錄後,在其他人。但我一直在獲得同樣的期望。 我做錯了什麼? – mXX