2012-04-24 13 views
1
package com.callout.project; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

import com.callout.project.Mylocation.LocationResult; 



import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Intent; 
import android.location.Location; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class WorksActivity extends Activity { 
/** Called when the activity is first created. */ 
Mylocation myLocation = new Mylocation(); 
TextView rtv; 
String id; 
String strloc; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.register); 

    Bundle ext = getIntent().getExtras(); 
     id = (String) ext.get("registration_id"); 
     Log.w("rid", id); 
    findCurrentLocation(); 
    rtv = (TextView)findViewById(R.id.rtv); 
    rtv.setText("Thank you For Registering"); 
    String eol = System.getProperty("line.separator"); 
    BufferedReader input = null; 
    try { 
     input = new BufferedReader(new InputStreamReader(
     openFileInput("myfile"))); 
     String line; 
     StringBuffer buffer = new StringBuffer(); 
     while ((line = input.readLine()) != null) { 
     buffer.append(line + eol); 
     Log.w("Hello","123"+line); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
    if (input != null) { 
     try { 
    input.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     } 
    } 

//Log.w("file","msg"+line); 


// sendtoserver(id,strloc); 
} 

private void findCurrentLocation() { 
    myLocation.getLocation(this, locationResult); 
} 
public LocationResult locationResult = new LocationResult() { 

    @Override 
    public void gotLocation(Location location) { 
     // TODO Auto-generated method stub 
     if (location != null) { 
      strloc = location.getLatitude() + "," 
        + location.getLongitude(); 
      Log.w("works",strloc); 


    // TODO Auto-generated method stub 
    AlertDialog malert = new AlertDialog.Builder(WorksActivity.this).create(); 
     malert.setTitle("here"+strloc);  
     Log.w("func","in func"+id); 
    HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.funnystrippers.co.cc/test.php"); 

     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4); 
      nameValuePairs.add(new BasicNameValuePair("Rid", "56")); 
      nameValuePairs.add(new BasicNameValuePair("location","strloc")); 
      nameValuePairs.add(new BasicNameValuePair("content", "hello frm android")); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      HttpResponse response = httpclient.execute(httppost); 
      Log.w("res",response.toString()); 

     } catch (ClientProtocolException e) { 
     } catch (IOException e) { 
     } 

     malert.show(); 
} 
     } 
    }; 

} Androidのファイルを読む

I C2DM、その作業罰金を扱うこの次のコードを持っていますが、私はLog.w( "こんにちは"、 "123" +ライン)の値が欲しいです。 try {} キャッチブロックの外側に印刷するとエラーが出るLogCatで印刷された値を取得する必要があります。

答えて

1

lineは、内部で宣言されているのでtryブロックを離れると有効範囲外になります。

これを修正するには、lineの宣言をtryブロックの外側に移動するだけで、ブロックを離れると表示されます。

String line; 
try { 
    input = new BufferedReader(new InputStreamReader(
    openFileInput("myfile"))); 
    StringBuffer buffer = new StringBuffer(); 
+1

ありがとう!!!私は言及したように変更したが、依然として 'NULL'を得ている。私は別の関数を作成し、 'line'の値を使うことを考えていました。もしそうなら、Try Catchブロックの外で! – gaurav