# Problem mit Cell id



## Progger (12. Jun 2012)

Hallo, 
ich habe heute versucht folgendes Programm von dieser Seite 
Android Coding: Get location of Cell ID, from opencellid.org using HttpGet(). zu starten, aber es kommt dann beim
Programmstart immer die Meldung, dass es leider stoppen musste.

Liegt es vll. daran, dass es im Emulator nicht funktioniert (kann es momentan nur da testen), oder ist es ev.
doch ein Rechteproblem. Ich habe das hier im Manifest eingetragen:


```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    
    
    package="com.cell"
    android:versionCode="1"
    android:versionName="1.0" >
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
	<uses-permission android:name="android.permission.READ_PHONE_STATE" />
	<uses-permission android:name="android.permission.INTERNET" />

    <uses-sdk android:minSdkVersion="8" />
	
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".CellActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
	
</manifest>
```

Würde gerne bei OpenCellID beitragen ohne einen Client von deren Homepage verwenden zu müssen.

Vielen Dank schon mal... 
Lg


----------



## schlingel (13. Jun 2012)

Debugge mit dem Logcat, dann weißt du mehr!


----------



## Progger (13. Jun 2012)

Danke schon mal...
Hab den Inhalt von Logcat hier mal gepostet. Könnte es etwas mit den Berechtigungen zu tun haben? 


```
06-13 17:44:44.266: I/dalvikvm(556): threadid=3: reacting to signal 3
06-13 17:44:44.595: I/dalvikvm(556): Wrote stack traces to '/data/anr/traces.txt'
06-13 17:44:44.765: V/DroidNova(556): mint Value:0
06-13 17:44:44.785: I/dalvikvm(556): threadid=3: reacting to signal 3
06-13 17:44:44.825: D/AndroidRuntime(556): Shutting down VM
06-13 17:44:44.835: W/dalvikvm(556): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-13 17:44:44.835: I/dalvikvm(556): Wrote stack traces to '/data/anr/traces.txt'
06-13 17:44:44.875: E/AndroidRuntime(556): FATAL EXCEPTION: main
06-13 17:44:44.875: E/AndroidRuntime(556): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cell/com.cell.CellActivity}: java.lang.NullPointerException
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.os.Looper.loop(Looper.java:137)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.app.ActivityThread.main(ActivityThread.java:4424)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at java.lang.reflect.Method.invokeNative(Native Method)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at java.lang.reflect.Method.invoke(Method.java:511)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at dalvik.system.NativeStart.main(Native Method)
06-13 17:44:44.875: E/AndroidRuntime(556): Caused by: java.lang.NullPointerException
06-13 17:44:44.875: E/AndroidRuntime(556): 	at com.cell.CellActivity.onCreate(CellActivity.java:130)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.app.Activity.performCreate(Activity.java:4465)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-13 17:44:44.875: E/AndroidRuntime(556): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-13 17:44:44.875: E/AndroidRuntime(556): 	... 11 more
06-13 17:44:45.286: I/dalvikvm(556): threadid=3: reacting to signal 3
06-13 17:44:45.296: I/dalvikvm(556): Wrote stack traces to '/data/anr/traces.txt'
06-13 17:44:45.455: I/dalvikvm(556): threadid=3: reacting to signal 3
06-13 17:44:45.475: I/dalvikvm(556): Wrote stack traces to '/data/anr/traces.txt'
06-13 17:49:44.946: I/Process(556): Sending signal. PID: 556 SIG: 9
```


----------



## schlingel (16. Jun 2012)

Der Log sagt, dass du eine NPE im onCreate hast.

Ohne Code vom onCreate kann man nicht viel sagen aber da eh die Zeilennummer dabei steht, kommst du vielleicht so schon weiter.


----------



## Progger (19. Jun 2012)

Danke für den Hinweis...leider hat sich das Problem noch nicht gelöst.

Ich hier den Code von der Seite nochmal gepostet...vll. ist es ja etwas offensichtliches...
Andere Frage: Da es mir ja nur um die Abfrage der Cell Id geht, würde ich gern wissen ob jemand ein Code Beispiel
hat dass bei ihm funktioniert hat, weil sämtliche Beispiele die ich probiert habe hatten dieses Problem.



```
package com.cell;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;

import android.util.Log;

public class CellActivity extends Activity {
	
	public static final String LOG_TAG = "DroidNova";

 public class OpenCellID {
  String mcc;  //Mobile Country Code
  String mnc;  //mobile network code
  String cellid; //Cell ID
  String lac;  //Location Area Code
  
  Boolean error;
  String strURLSent;
  String GetOpenCellID_fullresult;
  
  String latitude;
  String longitude;
  
  public Boolean isError(){
   return error;
  }
  
  public void setMcc(String value){
   mcc = value;
  }
  
  public void setMnc(String value){
   mnc = value;
  }
  
  public void setCallID(int value){
   cellid = String.valueOf(value);
  }
  
  public void setCallLac(int value){
   lac = String.valueOf(value);
  }
  
  public String getLocation(){
   return(latitude + " : " + longitude);
  }
  
  public void groupURLSent(){
   strURLSent =
    "http://www.opencellid.org/cell/get?mcc=" + mcc
    +"&mnc=" + mnc
    +"&cellid=" + cellid
    +"&lac=" + lac
    +"&fmt=txt";
  }
  
  public String getstrURLSent(){
   return strURLSent;
  }
  
  public String getGetOpenCellID_fullresult(){
   return GetOpenCellID_fullresult;
  }
  
  public void GetOpenCellID() throws Exception {
   groupURLSent();
   HttpClient client = new DefaultHttpClient();
   HttpGet request = new HttpGet(strURLSent);
   HttpResponse response = client.execute(request);
   GetOpenCellID_fullresult = EntityUtils.toString(response.getEntity()); 
   spliteResult();
  }
  
  private void spliteResult(){
   if(GetOpenCellID_fullresult.equalsIgnoreCase("err")){
    error = true;
   }else{
    error = false;
    String[] tResult = GetOpenCellID_fullresult.split(",");
    latitude = tResult[0];
    longitude = tResult[1];
   }
    
   
  }
 }

 int myLatitude, myLongitude;
 OpenCellID openCellID;

 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       
       int mInt = 0;
       Log.v(LOG_TAG, "mint Value:" + mInt);
       
       TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
       TextView textMCC = (TextView)findViewById(R.id.mcc);
       TextView textMNC = (TextView)findViewById(R.id.mnc);
       TextView textCID = (TextView)findViewById(R.id.cid);
       TextView textLAC = (TextView)findViewById(R.id.lac);
       TextView textGeo = (TextView)findViewById(R.id.geo);
       TextView textRemark = (TextView)findViewById(R.id.remark);
      
       //retrieve a reference to an instance of TelephonyManager
       TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
       GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
      
       String networkOperator = telephonyManager.getNetworkOperator();
       String mcc = networkOperator.substring(0, 3);
       String mnc = networkOperator.substring(3);
       textMCC.setText("mcc: " + mcc);
       textMNC.setText("mnc: " + mnc);
      
       int cid = cellLocation.getCid();
       int lac = cellLocation.getLac();
       textGsmCellLocation.setText(cellLocation.toString());
       textCID.setText("gsm cell id: " + String.valueOf(cid));
       textLAC.setText("gsm location area code: " + String.valueOf(lac));
      
       openCellID = new OpenCellID();
      
       openCellID.setMcc(mcc);
       openCellID.setMnc(mnc);
       openCellID.setCallID(cid);
       openCellID.setCallLac(lac);
       try {
           openCellID.GetOpenCellID();
   
           if(!openCellID.isError()){
               textGeo.setText(openCellID.getLocation());
               textRemark.setText( "\n\n" + "URL sent: \n" + openCellID.getstrURLSent() + "\n\n" + "response: \n" + openCellID.GetOpenCellID_fullresult);
           }else{
               textGeo.setText("Error");
           }
       } 
       catch (Exception e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
           textGeo.setText("Exception: " + e.toString());
       }
   }   
}
```

Quelle: Android Coding: Get location of Cell ID, from opencellid.org using HttpGet().


----------



## schlingel (19. Jun 2012)

Wieso machst du das nicht mit einem normalen LocationListener anstatt über die Cell-ID?

PS: Kann es sein, dass du auf einem Emulator testest? Denn dort bekommst du wenn dann nur eine Fake-Location die du selbst einstellen musst. Ansonsten ist die Location nicht erreichbar und laut Doku gibt dann der TelephonyManager null zurück.

Das heißt übrigens das der Code nicht defensiv genug programmiert ist, wie du gerade am eigenen Leib erfährst und zum anderen würde es mich interessieren wo der Vorteil davon ist, dass so selbst zu machen wenn's anders bequemer geht.


----------



## Progger (24. Jun 2012)

Ah ok, dass war das Problem...
mit einer zusätzlichen if Abfrage funktioniert es jetzt.


```
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;

import android.util.Log;

public class CellActivity extends Activity {
	
	public static final String LOG_TAG = "DroidNova";

 public class OpenCellID {
  String mcc;  //Mobile Country Code
  String mnc;  //mobile network code
  String cellid; //Cell ID
  String lac;  //Location Area Code
  
  Boolean error;
  String strURLSent;
  String GetOpenCellID_fullresult;
  
  String latitude;
  String longitude;
  
  public Boolean isError(){
   return error;
  }
  
  public void setMcc(String value){
   mcc = value;
  }
  
  public void setMnc(String value){
   mnc = value;
  }
  
  public void setCallID(int value){
   cellid = String.valueOf(value);
  }
  
  public void setCallLac(int value){
   lac = String.valueOf(value);
  }
  
  public String getLocation(){
   return(latitude + " : " + longitude);
  }
  
  public void groupURLSent(){
   strURLSent =
    "http://www.opencellid.org/cell/get?mcc=" + mcc
    +"&mnc=" + mnc
    +"&cellid=" + cellid
    +"&lac=" + lac
    +"&fmt=txt";
  }
  
  public String getstrURLSent(){
   return strURLSent;
  }
  
  public String getGetOpenCellID_fullresult(){
   return GetOpenCellID_fullresult;
  }
  
  public void GetOpenCellID() throws Exception {
   groupURLSent();
   HttpClient client = new DefaultHttpClient();
   HttpGet request = new HttpGet(strURLSent);
   HttpResponse response = client.execute(request);
   GetOpenCellID_fullresult = EntityUtils.toString(response.getEntity()); 
   spliteResult();
  }
  
  private void spliteResult(){
   if(GetOpenCellID_fullresult.equalsIgnoreCase("err")){
    error = true;
   }else{
    error = false;
    String[] tResult = GetOpenCellID_fullresult.split(",");
    latitude = tResult[0];
    longitude = tResult[1];
   }
    
   
  }
 }

 int myLatitude, myLongitude;
 OpenCellID openCellID;

 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       
       int mInt = 0;
       Log.v(LOG_TAG, "mint Value:" + mInt);
       
       TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
       TextView textMCC = (TextView)findViewById(R.id.mcc);
       TextView textMNC = (TextView)findViewById(R.id.mnc);
       TextView textCID = (TextView)findViewById(R.id.cid);
       TextView textLAC = (TextView)findViewById(R.id.lac);
       TextView textGeo = (TextView)findViewById(R.id.geo);
       TextView textRemark = (TextView)findViewById(R.id.remark);
      
       //retrieve a reference to an instance of TelephonyManager
       TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
       GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
      
       if (cellLocation instanceof GsmCellLocation){
       
    	   String networkOperator = telephonyManager.getNetworkOperator();
    	   String mcc = networkOperator.substring(0, 3);
    	   String mnc = networkOperator.substring(3);
    	   textMCC.setText("mcc: " + mcc);
    	   textMNC.setText("mnc: " + mnc);
      
    	   int cid = cellLocation.getCid();
    	   int lac = cellLocation.getLac();
    	   textGsmCellLocation.setText(cellLocation.toString());
    	   textCID.setText("gsm cell id: " + String.valueOf(cid));
    	   textLAC.setText("gsm location area code: " + String.valueOf(lac));
      
       
       
       
    	   openCellID = new OpenCellID();
      
    	   openCellID.setMcc(mcc);
    	   openCellID.setMnc(mnc);
    	   openCellID.setCallID(cid);
    	   openCellID.setCallLac(lac);
    	   try {
    		   openCellID.GetOpenCellID();
   
    		   if(!openCellID.isError()){
    			   textGeo.setText(openCellID.getLocation());
    			   textRemark.setText( "\n\n"
    				   + "URL sent: \n" + openCellID.getstrURLSent() + "\n\n"
    				   + "response: \n" + openCellID.GetOpenCellID_fullresult);
    		   }else{
    			   textGeo.setText("Error");
    		   }
    	   } 
    	   catch (Exception e) {
    		   // TODO Auto-generated catch block
    		   e.printStackTrace();
    		   textGeo.setText("Exception: " + e.toString());
    	   }
       
       }   
   }
}
```

Quelle: Android Coding: Get location of Cell ID, from opencellid.org using HttpGet().

Hab aus Interesse an der Android Api die Clients nicht von dort genommen...

Vielen Dank nochmal für die Hilfe.


----------

