Android文件读取

时间:2012-04-24 15:18:49

标签: android try-catch android-file

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

}

我有以下代码处理C2DM,它的工作正常,但我想要Log.w的值(“Hello”,“123”+ line);在try {}之外打印 当我这样做时会遇到阻塞它会给我错误我应该在我的LogCat中打印出值!!!

1 个答案:

答案 0 :(得分:1)

离开try块后,

line超出范围,因为它已在其中声明。

要修复它,只需将line的声明移到try块之外,一旦离开块,它仍然可见。

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